繁体   English   中英

FCM-单击系统任务栏通知时如何遍历特定活动

[英]FCM - How to traverse to a particular activity when system tray notification is clicked

我们正在使用FCM向终端用户显示推送通知。 FCM成功发出了通知,并且用户正在系统托盘中看到该通知。我正在使用FCM控制台Notification Composer进行测试。

我将我的应用程序保留在后台,并从FCM控制台发送消息。

要求是当用户单击任务栏消息时(在后台应用程序时)导航到应用程序中的特定活动。 现在,默认情况下,它会打开应用启动器页面。

我无法从google得到正确的答案。 大多数答案是针对GCM的,大多数不是可行的解决方案。 我在FCM页面中找不到正确的文档。

在后台应用程序中处理通知消息

当您的应用程序在后台运行时,Android会将通知消息定向到系统任务栏。 默认情况下,用户点击通知会打开应用启动器。

FCM page - 

这包括同时包含通知和数据有效负载的消息(以及从Notifications控制台发送的所有消息)。 在这些情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图之外传递。

谢谢

您必须在AndroidManifest.xml内添加带有操作的<intent-filter>并在通知有效负载中设置click_action ,然后它才能在应用程序处于后台或被click_action时起作用。

您可以参考以下链接:

从Firebase通知中打开特定活动

将此代码放在onMessageReceived()方法中

 private void sendNotification(String messageBody) {
            Intent intent = new Intent(this, YOUR_TARGET_ACTIVITY.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle("FCM Message")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

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

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }

您需要自己处理推送,并且不允许Google自动添加状态通知。 默认情况下,Firebase控制台将为您接管状态通知的交付,并且不会发生其他事情。 在Firebase接收器中,只需根据推送中的数据模式(您设置的) 构建自己的通知 ,然后将PendingIntent添加到通知中即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM