首页 > 解决方案 > 在 Android 中获取应用程序启动时的所有远程通知

问题描述

我需要notifications在应用程序启动时从 android 通知中心收集所有远程(在应用程序未运行时收到)。

我尝试过使用该NotificationManager.getActiveNotifications方法,但这给出了StatusBarNotification. 有一种方法getNotification()可用,StatusBarNotification但这也没有远程通知内容。

我试过这样的事情,

 NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
for (int i = 0; i < notifications.length ; i++) {
    Notification notification = notifications[i].getNotification();
    Log.d("Tag","Notification String = " + notification.toString());
}

输出: Notification String = Notification(channel=fcm_fallback_notification_channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)

请帮我提出您的建议。

非常感谢!

标签: androidandroid-notifications

解决方案


如果您想获取通知数据,请使用

Bundle bundle = notification.extras

这里的 bundle 将接收到的所有数据作为有效负载。因此,如果您想获得标题,请使用bundle.getString("android.title");


推荐阅读