首页 > 解决方案 > 如何在 Android 应用程序中显示来自推送通知的数据

问题描述

我有一个使用 FCM 推送通知的 android 应用程序。有一个发送推送通知的后端

每次用户上传图片时,都会向订阅该主题的人发送推送通知。

单击通知托盘中的通知后,我将被重定向到应用程序的第一页。

我怎样才能让推送通知像 Instagram 那样工作。

收到通知并单击它后,我想移至该特定屏幕并查看已上传的图像。

如果通知到达时用户在应用程序中。如何刷新活动以显示内容。我是否必须发出后端请求才能显示新数据,还是可以显示通知本身的内容?

有没有我可以看的例子。

任何帮助将不胜感激。谢谢你。

标签: androidfirebasefirebase-realtime-databasepush-notificationfirebase-cloud-messaging

解决方案


要启动一个包含一系列活动的活动,您需要创建一个 TaskStackBuilder 实例并调用 addNextIntentWithParentStack(),将要启动的活动的 Intent 传递给它。

只要您如上所述为每个活动定义了父活动,就可以调用 getPendingIntent() 来接收包含整个后退堆栈的 PendingIntent。

// Create an Intent for the activity you want to start
 Intent resultIntent = new Intent(this, ResultActivity.class);

// Create the TaskStackBuilder and add the intent, which inflates the back 
 stack

 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  stackBuilder.addNextIntentWithParentStack(resultIntent);

// Get the PendingIntent containing the entire back stack
  PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

推荐阅读