如何在点击 FCM 推送通知后打开特定活动。

huangapple go评论70阅读模式
英文:

How to open The specific Activity after clicking FCM push notification

问题

我尝试点击推送通知后打开特定活动,以下是代码:

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {

    private static final String CHANNEL_ID = "my_notification";
    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d("Token:", s);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        // 检查消息是否包含通知负载。
        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String message = remoteMessage.getNotification().getBody();
            Log.d("msg:", message);
            // 将通知数据传递给NotificationActivity。
            Intent intent = new Intent(this, NotificationActivity.class);
            intent.putExtra("title", title);
            intent.putExtra("message", message);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntentWithParentStack(intent);
            PendingIntent pendingIntent =
                    stackBuilder.getPendingIntent(0,
                            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

            // 构建通知。
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setAutoCancel(true);
            builder.setContentIntent(pendingIntent);

            int notificationId = (int) System.currentTimeMillis();
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.notify(notificationId, builder.build());
        }
    }
}

这是我的AndroidManifest.xml文件的FCM配置:

<service
    android:name=".MyFirebaseInstanceIDService"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

<meta-data
    android:name="com.google.firebase.messaging.default_notification_click_action"
    android:value=".NotificationActivity" />

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id" />

请注意,我只提供了代码的翻译部分,没有包括额外的内容。

英文:

I have tried to open the specific activity after clicking the push notification

Here is the code

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String CHANNEL_ID = &quot;my_notification&quot;;
private static final int NOTIFICATION_ID = 1;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d(&quot;Token:&quot;, s);
}
@SuppressLint(&quot;MissingPermission&quot;)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Check if the message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Log.d(&quot;msg:&quot;, message);
// Pass the notification data to the NotificationActivity.
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra(&quot;title&quot;, title);
intent.putExtra(&quot;message&quot;, message);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// Build the notification.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
int notificationId = (int) System.currentTimeMillis();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
}
}
}

Here is is my Androidmanifest.xml for fcm

 &lt;service
android:name=&quot;.MyFirebaseInstanceIDService&quot;
android:exported=&quot;true&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/service&gt;
&lt;meta-data
android:name=&quot;com.google.firebase.messaging.default_notification_click_action&quot;
android:value=&quot;.NotificationActivity&quot; /&gt;
&lt;meta-data
android:name=&quot;com.google.firebase.messaging.default_notification_channel_id&quot;
android:value=&quot;@string/default_notification_channel_id&quot; /&gt;

Here iam facing the issue like the NotificationActivity is not opening And the onMessageRecived function is not running in background

答案1

得分: 0

以下是您的代码的中文翻译部分:

public static void sendNotification(Context context, String title, String body, 
    Map&lt;String, String&gt; data, String userType) {
    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), 
        R.drawable.ic_my_taxi);
    Intent intent = new Intent(context, NotificationActivityDriver.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "channel_id")
        .setContentTitle(title)
        .setContentText(body)
        .setAutoCancel(true)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setContentIntent(pendingIntent)
        .setContentInfo(title)
        .setLargeIcon(icon)
        .setColor(Color.BLUE)
        .setLights(Color.BLUE, 1000, 300)
        .setDefaults(Notification.DEFAULT_VIBRATE)
        .setSmallIcon(R.drawable.ic_my_icon);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

Manifest文件如下

<service
    android:name="com.notification.test.service.MyFirebaseMessagingService"
    android:directBootAware="true"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
英文:

here is my code.

public static void sendNotification(Context context, String title, String body, 
Map&lt;String, String&gt; data, String userType) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), 
R.drawable.ic_my_taxi);
Intent intent = new Intent(context, NotificationActivityDriver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, &quot;channel_id&quot;)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setContentInfo(title)
.setLargeIcon(icon)
.setColor(Color.BLUE)
.setLights(Color.BLUE, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.ic_my_icon);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

Manifest file is below

 &lt;service
android:name=&quot;com.notification.test.service.MyFirebaseMessagingService&quot;
android:directBootAware=&quot;true&quot;
android:exported=&quot;false&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/service&gt;

huangapple
  • 本文由 发表于 2023年5月8日 00:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定