简体   繁体   中英

How to show push notification using firebase when app is destroyed

I have an app that consumes firebase messaging service to recieve push notifications.

I already searched for the differences between using "notification" or "data" while sending the notification information to the FCM sending server.

My notification doesn't appear when the app is destroyed, only while it is on foreground or background.

I need to make it so that my server ocassionally send a push notification to the device every once and a while (about twice a week or so) but the app should be able to display the notification while it is closed.

Any idea how I can do this? This is my notification json encoded string that i am sending to the FCM sending server:

{
"to":"d_kQKDkq5V4:APA91bG4hIyqbr2a_24nUIe6jVbySS90FMnVKwwuTfG1dV3OooeAc_555XkB2e_h_oWzEOMde8uIt2ESvv_Thl1J1sEXDHWPsLCE7EdMXkZ_AS6xlVq8uJIjVojz1WPxqiUjWZm65Ypf",
"data":{
            "title":"App in background again",
            "body":"asdsad"
       }
}

So the notification appears while the app is on background or foreground, but it doesn't appear while it is destroyed. How do i make it so it can still recieve and display the notification while the app is destroyed?

This is my onMessageRecieved() method:

public void onMessageReceived(RemoteMessage remoteMessage) {
        /*String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
          */
        Log.e("REG_TOKEN",remoteMessage.getData().get("title"));
        Map<String,String> data = remoteMessage.getData();
        String title = data.get("title");
        String message = data.get("body");
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());


    }

我已经看到这种方法不适用于定制OS手机。

It's possible the behavior you are seeing is caused by your device being in Doze or Standby mode when the message is received. Because you are sending a data message, if you want the device to be awakened immediately to receive your message, you need to send it with high priority as explained in the documentation :

By default, notification messages are sent with high priority, and data messages are sent with normal priority. Normal priority optimizes the client app's battery consumption and should be used unless immediate delivery is required. For messages with normal priority, the app may receive the message with unspecified delay.

It's a one line addition to your message:

{
"to": "d_kQKDkq5V4:APA91bG4hIy...qiUjWZm65Ypf",
"priority": "high",
"data":{
            "title":"App in background again",
            "body":"asdsad"
       }
}

If sending the message with high priority doesn't help, then you may be testing on one of the models of phones that puts an app in Stopped State when it is killed in certain ways (such as swiping from the recent tasks list). See this answer for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM