应用程序在后台时的Firebase控制台消息传递

编程入门 行业动态 更新时间:2024-10-28 00:18:32
本文介绍了应用程序在后台时的Firebase控制台消息传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Firebase控制台(而不是api)的通知正文中发送JSON,然后在其中进行转换 onMessageReceived方法.当应用程序位于前台时,当应用程序位于后台时,此方法有效,不会调用onMessageReceived方法,并且通知主体在通知托盘中显示为字符串.

I am sending a JSON in notification body in Firebase Console not api, and then convert in it in onMessageReceived method. This approach is working when app is in foreground, when app is in background, onMessageReceived method is not get called and notification body is shown as string in notification tray.

有什么办法或解决方法吗?

Is there any way or workaround for this?

注意:由于具有定位功能,我正在通过控制台发送邮件.

Note: I am sending through console because of the Targeting feature.

推荐答案

来自官方文档

onMessageReceived为大多数消息类型提供,但以下情况除外:

在后台接收到的同时具有通知和数据有效负载的消息.在这种情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图之外传递.

Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

这意味着,默认情况下,通知将由android系统显示,您对此无能为力.但是,您可以在通知中附加键值数据有效负载,这些有效负载将作为活动的额外意图提供.您可以接收这些数据值并执行您想做的任何事情.

That means, the notification will by default be shown by android system, you can't do nothing about that. But You can attach key-value data payload with your notification, which will be delivered as intent extras of of activity. You can receive those data values and do whatever you want to do.

我已经创建了一个如下所示的辅助函数,并将其放在活动的onCreate()中

I have created a helper function like below and put it inside onCreate() of the activity

public void FCM() { if (getIntent().getExtras() != null) { String pack=(String) getIntent().getExtras().get(YOUR_KEY); if(pack!=null) { Log.e("fcm main pack", pack); try { Uri uri = Uri.parse("market://details?id=" + pack); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); startActivity(goToMarket); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("play.google/store/apps/details?id=" + pack))); } } } //else Log.e("fcm main","null"); }

更多推荐

应用程序在后台时的Firebase控制台消息传递

本文发布于:2023-11-10 06:47:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   台时   消息   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!