【问题标题】:Go to different activities when the FCM notification is clicked单击 FCM 通知时转到不同的活动
【发布时间】:2022-01-20 12:52:16
【问题描述】:

伙计们,我正在使用 FCM 向我的 android 应用程序发送通知,它工作正常,但是当我点击通知时,它会将我发送到我在此处设置的特定活动:

 @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, activity_togo_to.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "101")
                .setSmallIcon(R.drawable.ic_add)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
        notificationManager.notify(1, builder.build());
    }

我的问题我不想在点击通知时转到相同的活动。

所以我的通知系统是这样工作的:

我在 MySQL 数据库 A 和 B 中有两个表:

当A表添加一行时-->推送标题为:“有一个新项目A”

当 B 表添加一行时 --> 推送标题为:“有一个新项目 B”

当我点击通知时:

标题为:“有一个新项目A”-->转到活动A

标题为:“有一个新项目B”-->转到活动B

我怎样才能做到这一点,我真的需要它。

感谢任何帮助。 如果不可能,请告诉我。

【问题讨论】:

    标签: java android firebase firebase-cloud-messaging


    【解决方案1】:

    您的通知的待处理意图必须是活动 A 或 B。您可以将参数从远程服务器传递给您的通知到 remoteMessage.getData()。

    例如:

    with(remoteMessage.data) {
        // Extract data
        type = get(PARAM_TYPE)
        variation = get(PARAM_VARIATION)
        priority = getNotificationPriorityFromString(get(PARAM_PRIORITY))
        title = get(PARAM_TITLE)
        message = get(PARAM_MESSAGE)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多