首页 文章

FCM - 单击系统托盘通知时如何遍历特定活动

提问于
浏览
0

我们使用FCM向最终用户显示推送通知 . FCM成功提出通知,用户正在从系统托盘中看到通知 . 我正在使用FCM控制台通知编写器进行测试 .

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

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

我无法从谷歌得到正确的答案 . 大多数答案都是针对GCM的,大多数都不是解决方案 . 我在FCM页面中找不到合适的文档 .

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

当您的应用在后台时,Android会将通知消息定向到系统托盘 . 用户点按通知会默认打开应用启动器 .

FCM page -

这包括包含通知和数据有效负载的消息(以及从通知控制台发送的所有消息) . 在这些情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递 .

谢谢

3 回答

  • 0

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

    您可以参考以下链接:

    open a specific activity from firebase notification

  • 0

    将此代码放在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());
            }
    
  • 1

    您需要自己处理推送,并且不允许Google自动添加状态通知 . 默认情况下,Firebase控制台将为您接管状态通知的传递,并且不允许其他情况发生 . 在Firebase接收器中,只需build your own notification基于推送中的数据模式(您设置),并在通知中添加PendingIntent .

相关问题