如何在通知点击时打开 activity

How to open an activity on notification click

我正在使用 FCM 向用户发送自定义通知,现在我希望当用户点击通知时 activity 应该打开。

这是我在 mainActivity.java 中的代码片段:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("MyNotification", "MyNotification", NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }

下面是我的服务 class:

public class MyMessagingService extends FirebaseMessagingService {
@Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

    public void showNotification(String title, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.noti)
                .setAutoCancel(true)
                .setContentText(message);

        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.notify(999, builder.build());
    }
}

请告诉我如何实现。 提前致谢

           // Create an Intent for the activity you want to start
            Intent resultIntent = new Intent(this, ResultActivity.class);
            // Create the TaskStackBuilder and add the intent, which inflates the back stack
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntentWithParentStack(resultIntent);
            // Get the PendingIntent containing the entire back stack
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
            builder.setContentIntent(resultPendingIntent);

            NotificationManagerCompat notificationManager = 
            NotificationManagerCompat.from(this);
            notificationManager.notify(NOTIFICATION_ID, builder.build());

有关详细信息,请访问 https://developer.android.com/training/notify-user/navigation

编辑 这是你想要的代码

public class MyMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Intent activityIntent = new Intent(getApplicationContext(), YourActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), pendingIntent);
    }

    public void showNotification(String title, String message, PendingIntent pendingIntent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.noti)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setContentText(message);

        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.notify(999, builder.build());
    }
}

对于点击通知并转到特定 activity,您应该使用 Pending Intent。如下所示:

Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

然后像这样制作您的通知生成器

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.noti)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setContentText(message); 

所以你的 finally showNotification() 方法看起来像这样:

public void showNotification(String title, String message, PendingIntent pendingIntent) {

    Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.noti)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setContentText(message);

    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    manager.notify(999, builder.build());
}

如果有什么不明白的或者有疑问可以在这里评论。