单击 FCM 通知打开当前关闭 activity 未针对 activity

Clicking on FCM notification opens currently closed activity not targeted activity

这是我的代码,当我的应用 运行 在前台时,它会打开目标 activity 但是当应用关闭或在后台时,它不会打开目标 activity,请帮我解决这个问题

我希望通过点击通知将有针对性地打开 activity 即使应用程序 运行 / 已关闭。

 public void onMessageReceived(RemoteMessage remoteMessage) {

    session = new Session(this);
  //  if (session.getBscloggedin() || session.getAdploggedin()) {
       // Log.d(TAG, "FROM:" + remoteMessage.getFrom());

        //Check if the message contains data
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
        }

        //Check if the message contains notification

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }


/**
 * Dispay the notification
 * @param body
 */


private void sendNotification(String body) {

    //for CS101 announcement notification
    if (body.contains("CS101")) {
        Intent intent = new Intent(this, CS101noti.class);
        intent.putExtra("body", body);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, 0);
        //Set sound of notifica tion
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("mine")
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
    }

使用您发送推送通知的后端的“click_action”属性。在该属性值中,您必须传递 activity 的完全限定 class 路径,当您单击该 notification.for 时应打开该路径 更多参考参考 handling firebase push

Intent intent = new Intent(this, CS101noti.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
....
当您在后台收到通知消息时,

onMessageReceived 回调 不会 调用。如果您在后台收到此类消息,则只能在启动器中处理 activity。

解决方案是要么更改后端,使其仅发送数据消息并由您在内部处理,要么在您的启动器中编写一些路由代码 activity。

请求代码应该是唯一的。

通常人们与我们分享示例,例如提供请求代码 0(零)。有时我是 copy/paste 开发人员之一。但是最近我在使用这条线时遇到了错误。

PendingIntent.getActivity(myContext, 0, myIntent, 0);

只要应用收到通知,intent activity 就会收到旧数据,因为 requestCode。这是关于该方法的文档。

Returns an existing or new PendingIntent matching the given parameters.

所以第二个参数 (requestCode) 应该是唯一的意图。如果您对每个通知使用相同的请求代码,activity 将收到旧数据。所以你的requestCode应该是唯一的。

还有一个solution.UsePendingIntent.FLAG_UPDATE_CURRENT作为flag。医生说;

If the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.