且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-27 11:00:22

来自官方文档

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");
    }