Android Can';在FCM中单击通知时,不要启动特定活动的应用程序
我遇到了这个问题:如果我单击通知,我的应用程序关闭,Android Can';在FCM中单击通知时,不要启动特定活动的应用程序,android,firebase,Android,Firebase,我遇到了这个问题:如果我单击通知,我的应用程序关闭,HomeActivity将打开,而不是请求的应用程序 我提出的通知如下: Intent intent = new Intent(context, ArticleActivity_.class); intent.putExtra(ArticleActivity.KEY_ARTICLE_ID, articleId); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pend
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
将被打开,而不是我配置的 您遇到了这个问题,因为FCM就是这样工作的。推送通知有两种类型。第一类是“通知”,第二类是“数据”。两者都可以可视化为JSON负载:
"notification": {
//key values
}
"data": {
//your custom key values
}
第三种类型是两者的结合
当应用程序不在前台时,“通知”类型将创建具有默认行为的默认视觉通知。当应用程序打开时,它将在messagereceived上执行您在中编写的任何内容。第二种类型“数据”将始终执行您在onMessageReceived
中编写的代码
从Firebase web控制台,您可以发送“通知”类型或组合类型。要仅发送“数据”,您需要从Firebase功能或服务器(符合FCM要求)发送数据
你可以用一个技巧来实现你的目标。可从Firebase web控制台发送的组合负载包含“通知”和“数据”。“数据”键值在用户单击通知后可用。该信息作为打开默认启动器活动的意图的额外数据提供,因此您可以在那里更改活动
在启动程序活动中,添加以下内容:
String notification = getIntent().getStringExtra("your-key");
if (notification != null) {
startActivity(new Intent(this, YourActivity.class));
}
通过这种方式,您可以验证活动是否因为单击了默认通知而打开,并且始终是字符串,因为按规则推送FCM中的负载必须始终是字符串。在其他情况下,如果用户只是打开应用程序,则字符串将为空,并且您不会重定向用户。您的意思是希望在不同的通知中打开不同的活动单击?我的意思是我希望打开与HomeActivity不同的特定活动。这是firebase提供的吗?是,我收到来自FCM的通知并将其推送,用户点击我将打开具体活动尝试此