Firebase(FCM) notifications not coming when android app removed from recent tray
Asked Answered
C

3

8

I'm sending Firebase notifications through my own webservice with PHP like below.

$url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $tokens,
             'data' => $message
            );
        $headers = array(
            'Authorization:key = SERVER_KEY ',
            'Content-Type: application/json'
            );

1.Notifications are coming when App is in Foreground and Background without any issues , But if I removed app from recent tray then notifications not coming , I must have to handle this any solution for this ? (like Whatsup notifications are showing all scenarios even I force stopped this app from settings)

2.Once I received notification , from "onMessageReceived" how to pass these message,body content to my Activity /Fragments?

3.In some cases I want to send notification to all , Instead of adding all tokens to array . any other way to handle like "send to ALL" something like that ?

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Data Payload : " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().get("message"));
    }
    private void sendNotification(String messageBody) {
        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);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
} 

Failed devices: (Notifications are not coming after removed from Recent Tray)

  • MI Redmi Note 2 (5.0.2 Lollipop)
  • Huawei honor 5c (5.1.1 Lollipop)

Succeed devices: (Notifications are coming after removed from Recent Tray)

  • HTC One X (4.2 Jelly Bean)
  • Samsung galaxy grand prime (4.4 Kitkat)
Cuirbouilli answered 30/6, 2016 at 16:32 Comment(4)
I don't think this is an issue of the OS version (Lollipop vs Jelly Bean etc), it is more likely an issue of whether or not Google Play services is allowed to wake apps that have been dismissed from recients. Some manufactures adjust this behaviour. I'd suggest filing an issue with firebase support so the FCM team can look deeper into the devices that are not operating as expected. firebase.google.com/supportLever
I am also having the same problem with data messages. And as far as I can tell, it is not working for any version of Android. I have tested on Marshmallow and Lollipop.Teeny
lava Irish x1 alsoBorage
https://mcmap.net/q/245634/-android-fcm-not-receiving-notifications-when-app-is-removed-from-backgroundForge
C
5

1.Notifications are coming when App is in Foreground and Background without any issues , But if I removed app from recent tray then notifications not coming , I must have to handle this any solution for this ? (like Whatsup notifications are showing all scenarios even I force stopped this app from settings)

As per your code you must be getting a null pointer exception on remoteMessage.getNotification().getBody() as you are not sending notification, instead you are sending data payload from your curl request. As per my personal experience, the issue you just mentioned is seen especially in Android Kitkat or below devices. On Lollipop or above Firebase push seems to work fine.

2.Once I received notification , from "onMessageReceived" how to pass these message,body content to my Activity /Fragments?

You can issue a Broadcast from onMessageReceived() and register a Braodcast Receiver in your Activity and send the data in your broadcast intent. Example code:

Activity

private BroadcastReceiver receiver;

@Overrride
public void onCreate(Bundle savedInstanceState){

  // your oncreate code

  IntentFilter filter = new IntentFilter();
  filter.addAction("SOME_ACTION");

  receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String message = intent.getStringExtra("message");
    }
  };
     registerReceiver(receiver, filter);
}

 @Override
 protected void onDestroy() {
  if (receiver != null) {
   unregisterReceiver(receiver);
   receiver = null
  }
  super.onDestroy();
 }

FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());

        Intent intent=new Intent();
        intent.setAction("SOME_ACTION");
        intent.putExtra("message", remoteMessage.getNotification().getBody());
        sendBroadcast(intent);
    }
} 

3.In some cases I want to send notification to all , Instead of adding all tokens to array . any other way to handle like "send to ALL" something like that ?

There is nothing like "Send to ALL" in Firebase API, the only way to do this is using Firebase Console which have its own issues to handle like white icon issue, and ripping the custom params of your notification.

Casaleggio answered 30/6, 2016 at 18:49 Comment(2)
About the question 3: i think it is possible to "send all" if you register each user to a "topic", no?!Rao
@Hisham I just edited my post with tested devices ,In my case pre-lollipop devices are working fine that mean notifications are coming after removed app from recent tray but in Lollipop device notifications are not coming when removed app , Any clue about this ? Also edited sendNotification(remoteMessage.getData().get("message"));Cuirbouilli
N
1

I was having problem with FCM on exactly Mi Phone and Huawei phone as well! Turns out that it was not a problem with FCM or your app, but on their device level.

They have device level app settings that disable notifications or even wake lock, so onMessageReceived() is not fired when the message arrives. After I changed the settings (mainly allow notifications & disable battery optimisation for my app), everything works just fine!

Refer to Firebase cloud messaging not received when app in background.

Nuthatch answered 11/10, 2016 at 16:21 Comment(0)
B
-1

This problem needs back-end implementation. Let me discuss how I overcome this shortcoming.

We are aware of the Activity life-cycle. So upon destroy, I call a service from the back-end that unregisters my client token. All incoming messages will now be queued for delivery since the token for target client is unregistered.

So upon bringing back the application, I call a back-end service that registers my client. The back end now starts to sends messages from queue with the new client token for my device.

Brambling answered 27/1, 2017 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.