且构网

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

应用程序被杀死时,Android Firebase推送通知不起作用

更新时间:2023-01-03 14:15:31

按照

As per the doc there are two possibilities when notification arrived.

  1. 应用程序处于前台.

您将在onMessageReceived中获得通知,并且可以通过此方法获取数据.您需要在系统任务栏中生成本地通知.

You will get notify in onMessageReceived, and you can get data in this method. You need to generate local notification in system tray.

处理通知

您已经完成了上述代码.

You have done this code as mentioned in question.

  1. 应用程序处于后台.

您会在系统任务栏中收到通知.您无需在此处生成通知.您将在启动器活动的意图中获取数据.

You get notification in system tray. You don't need to generate notification here. You will get data in Intent of launcher Activity.

处理通知

这是启动器活动的代码

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (intent == null || intent.getExtras() == null) {
            // your code to handle if there is no notification
        } else {
            // handle notification
            String body = intent.getStringExtra("body");
            String title = intent.getStringExtra("title");
            //TODO your code to open activity as per notification
        }
    }

我已经做到了,它对我来说也有用.

I have done this, and its works for me.