我正在将Firebase Cloud Messaging用于Android远程通知,以下代码供您参考。我想单击“通知”以打开特定活动,并希望在“警报”框中显示通知消息。
但是当我单击“通知”时,它将打开“主要活动”。在代码中,您可以看到我正在传递 Other.class ,但仍然无法正常工作...
我错过了什么吗?代码有什么问题吗?
还可以帮助我实现第二个功能,即如何在AlertBox /对话框中显示通知消息
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent=new Intent(this,Other.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);//new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("FCM NOTIFICATION");
notificationBuilder.setContentText(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.notify(0,notificationBuilder.build());
}
}