【问题标题】:Can't start Application to particular activity on Notification click from FCM无法启动应用程序到特定活动的通知单击从 FCM
【发布时间】:2017-08-18 14:21:48
【问题描述】:

我遇到了这个问题:如果我点击通知并关闭我的应用程序,HomeActivity 会打开而不是请求的。

我是这样提出通知的:

Intent intent = new Intent(context, ArticleActivity_.class);
intent.putExtra(ArticleActivity.KEY_ARTICLE_ID, articleId);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , intent,
            PendingIntent.FLAG_UPDATE_CURRENT);


Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

Notification n = notificationBuilder.build();
n.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 , n);

如果应用程序打开,一切正常。但是如果我关闭它,点击通知后,HomeActivity 将被打开,而不是我配置的那个。

【问题讨论】:

  • 您的意思是希望在不同的通知点击中打开不同的活动?
  • 我的意思是我想打开一些不同于 HomeActivity 的特定活动。
  • 是来自 firebase 的吗?
  • 是的,我收到了来自 FCM 的通知并推送它,用户点击我将打开具体的活动

标签: android firebase


【解决方案1】:

您遇到此问题是因为 FCM 就是这样工作的。推送通知有 2 种类型。第一种是“通知”,第二种是“数据”。两者都可以可视化为 JSON 有效负载:

"notification": {
    //key values
}

"data": {
    //your custom key values
}

第三种类型是两者的结合。

“通知”类型将在应用不在前台时创建具有默认行为的默认视觉通知。当应用程序打开时,它将执行您在onMessageReceived 中写入的任何内容。第二种“数据”将始终执行您在 onMessageReceived 中编写的代码。

从 Firebase 网络控制台,您可以发送“通知”类型或组合类型。要仅发送“数据”,您需要从 Firebase 函数或服务器(符合 FCM 要求)发送。

您可以采取一些技巧来实现目标。可以从 Firebase Web 控制台发送的组合有效负载包含“通知”和“数据”。 “数据”键值在用户单击通知后可用。该信息可作为 Intent 打开默认启动器 Activity 的额外数据使用,因此您可以更改您的 Activity。

在您的启动器活动中,添加以下内容:

String notification = getIntent().getStringExtra("your-key");
if (notification != null) {
    startActivity(new Intent(this, YourActivity.class));
}

通过这种方式,您可以验证 Activity 是否正在打开,因为单击了默认通知,并且始终为字符串,因为推送 FCM 中的有效负载必须按规则始终为字符串。在其他情况下,如果用户只是打开了应用程序,那么 String 将为 null,并且您不会重定向用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2020-08-24
    • 2019-05-21
    • 2022-01-20
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多