0

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());


    }
KENdi
  • 7,576
  • 2
  • 16
  • 31

2 Answers2

0

I have seen this approach does not work on custom OS phones.

Harsh Mittal
  • 2,868
  • 1
  • 16
  • 21
0

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.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I added the priority but nothing changed i still can't recieve the notification. then I starte dlooking at the Stopped State questions and answer here on stackoverflow but i don't seem to get which classes I need to actually make this work. Do I need to use BroadcastReciever? – Christian Wolf Alves Hess Jun 16 '17 at 13:00
  • After you kill the app, go to _Settings_ > _App Manager_. Find the app and look at the App Info. Is the _Force Stop_ button enabled or disabled? If disabled, the app is in Stopped State and will not receive FCM messages until the user takes it out of Stopped State by opening it. – Bob Snyder Jun 16 '17 at 13:13
  • the force stop is not disable, I can still click it. So that means that it isn't in Stopped State? – Christian Wolf Alves Hess Jun 16 '17 at 13:45
  • Right. What model phone are you using? And what API level? – Bob Snyder Jun 16 '17 at 13:48
  • When you don't receive the notification, do you see the `REG_TOKEN` log message from `onMessageReceived()`? – Bob Snyder Jun 16 '17 at 13:51
  • NO I don't. That's why I think it is in the stopped state even if I still can click the button. The notification is not arriving while the app is destroyed. – Christian Wolf Alves Hess Jun 16 '17 at 14:01
  • OK i just tested it in another phone and it worked with the app completely shutdown. So the problem was my phone after all. But in this case i can't give the certainty that each and every phone will be able to show the push notifications. Then, do you have any idea how, say, Whatsapp or Telegram do it so that it appears every single time? – Christian Wolf Alves Hess Jun 16 '17 at 14:12
  • I've not seen that explained well. Some posters have speculated that on phones that are blocking message receipt after kill, widely used apps, like Whatsapp, are "whitelisted" and exempt from the blocking. – Bob Snyder Jun 16 '17 at 14:19
  • If you are testing on a device running API 23+, one other thing to explore is [exempting the app from Doze/Standby](https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases). This shouldn't be necessary if you are sending the messages with high prioirtity. – Bob Snyder Jun 16 '17 at 14:25