应用程序在后台时的Firebase控制台消息传递 [英] Firebase Console Messaging when app is in background

查看:55
本文介绍了应用程序在后台时的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("https://play.google.com/store/apps/details?id=" + pack)));
                }
            }



        }
        //else Log.e("fcm main","null");
    }

这篇关于应用程序在后台时的Firebase控制台消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆