1

I am trying to implement FCM in my Android app. On a few phones, the notification is received when the app is in foreground/background/killed. But on some phones, it is only received when the app is in foreground/background. When the app is killed, the onMessageReceived and the launcher getExtra is also not called. I am using both the data and notification object from the API. Here is my Android code -

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("Here", "here");
if (remoteMessage.getData().size() > 0) {
  Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
  try {
    JSONObject json = new JSONObject(remoteMessage.getData());
    sendPushNotification(json);
  } catch (Exception e) {
    Log.e(TAG, "Exception: " + e.getMessage());
  }
}
if (remoteMessage.getNotification() != null) {
  Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
Ambuj Kathotiya
  • 369
  • 4
  • 18

4 Answers4

1

Add BootBroadcastReceiver in Manifest file

<receiver
            android:name=".OnBootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

Create Following Java file. call the FirebaseMessagingReceiveService inside it.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
        i.setClass(context, FirebaseMessagingReceiveService.class);
        context.startService(i);
    }
}
jessica
  • 1,700
  • 1
  • 11
  • 17
1

In custom ROM devices when app is killed by user, all services are also killed ,therefore no notifications

In order to overcome this we have to use data notifications

From your server side send data notification like following format:

{
 "to" : "your_token",

 "time_to_live" :4500,
 "restricted_package_name":"your_package_name"
 
 
 "data" : {
     "body" : "Sending Notification Body From Data",
     "title": "Notification Title from Data",
     
     
     
 }
 "direct_boot_ok" : true,
 "priority" : "high"

}

I tested this solution on devices like MI,One Plus, OPPO and notifications are received even when app is killed/background

milton titus
  • 179
  • 1
  • 5
0

I bumped into the same issue with notification and after researching we discovered that the issue is device specific, different device manufactures will have different handling of not only notifications but other services too such as VPN.

  • Unfortunately its by design by the device manufacture

Killing the App will also kill those services, you will have to educate end users that your app doesn't work on a list of devices, the one device that always returns positive results for as is Samsung device.

Cheers.

alkathirikhalid
  • 887
  • 12
  • 18
  • There must be a way around this. Because the same device receives notifications from InShorts even when the app is killed. – Ambuj Kathotiya Sep 07 '17 at 09:25
  • Hi, based on your question and description "On a few phones, the notification is received when the app is in foreground/background/killed. But on some phones, it is only received when the app is in foreground/background." that would mean that specific device blocks the push notifications as explained in my answer... If in Shorts you mean while killing the app or few seconds after killing the App that means the process of receiving the notification just completed, killing an app doesn't mean every process or service shuts down instantly. if you find my answer useful do vote it up, cheers. – alkathirikhalid Sep 07 '17 at 09:31
  • There is an app called InShorts. It is able to push notification even when the app is killed. – Ambuj Kathotiya Sep 07 '17 at 09:34
  • Is there any update/solution for this? How facebook and whatsapp is managing it? – Sagar Panwala Oct 14 '17 at 12:11
  • As of Android API 26 and above, you can not start a service unless the app is in foreground.Source: https://developer.android.com/guide/components/services – alkathirikhalid Dec 14 '18 at 11:07
  • https://medium.freecodecamp.org/why-your-push-notifications-never-see-the-light-of-day-3fa297520793 – DKV Dec 16 '18 at 08:28
0

I have come across similar problem too. Although some sources say that this is an issue of firebase, it more about the manufacturer who does not use Android's native ROM.

In simple words manufactures like One Plus, MI, Oppo, Vivo, etc use customised ROM(rewrite the behaviour of stock Android). So when the app is killed by the user, all its services are also killed hence no notifications. One way to solve this is to make the users change their settings.

Original Article can be found here.

Abhinav Upadhyay
  • 140
  • 1
  • 14