我正在使用FCM向用户发送自定义通知,现在我希望当用户单击通知时应打开一个活动。
这是我来自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);
}
以下是我的服务类别:
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());
}
}
请告诉我如何实现此目标。 预先感谢
答案 0 :(得分:1)
// 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());
}
}
答案 1 :(得分:1)
要获得点击通知并进行特定活动,您应该使用“待定意图”。如下所示:
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
然后使您的Notification Builder像这样
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
因此,您的最终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());
}
如果您不理解或有任何疑问,请在此处评论。