1

I followed official sample and extends FirebaseMessagingService

<service android:name=".service.MyFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

and my push json

{
"message":{
    "token":"my fcm token",
    "android":{
        "priority": "high",
        "data":{
            "key1":"123","key2":"456"
        }
    }
}

}

I reveived fcm from

@Override public void onMessageReceived(RemoteMessage remoteMessage) {}

until I killed my app from recent application.

My android os is Android 8.1 (API level 27)

I have test on android 7.1(API level 25), it can receive message, even app got killed.

KENdi
  • 7,576
  • 2
  • 16
  • 31
youteng li
  • 53
  • 1
  • 12
  • firebase messaging service will only get called if the app is in the background. Once the app is completely killed only a foreground notification will be shown when applicable – SteelToe Mar 19 '19 at 03:21
  • yes, beware of the two types of fcm message, foreground/background, they receive different parts of the data and handled in different ways. If your app is killed, it will be handled by system. System will only read specific columns. – Wesely Mar 19 '19 at 03:36
  • @Wesely do you mean using `notification` key? I tried but it still not received anything. – youteng li Mar 19 '19 at 05:34
  • Which device you are using for testing notification? – primo Mar 19 '19 at 06:17
  • @primo I have two device got same result. 1 oppo r17 android 8.1. 2 asus rog phone android 8.1. – youteng li Mar 19 '19 at 06:22
  • can you please tell me which OS both the phones have? – primo Mar 19 '19 at 06:39
  • @primo both two are android 8.1(api level 27) – youteng li Mar 19 '19 at 06:44
  • i'm asking you the OS version not android version – primo Mar 19 '19 at 06:44
  • You will get it in settings->About phone. You will get "colorOs version " or something else – primo Mar 19 '19 at 06:48
  • check this link https://stackoverflow.com/questions/52736782/fcm-push-notification-not-working-when-app-close-in-some-of-the-devices-like-xia?noredirect=1&lq=1 and this link https://stackoverflow.com/a/52943903/8101634 – primo Mar 19 '19 at 06:56
  • @primo `oppo r17 colorOs is v5.2.1`, asus not found. – youteng li Mar 19 '19 at 07:02
  • There is problem with such OS. Try allowing auto start app – primo Mar 19 '19 at 07:06
  • Provide ChannelID while triggering from FCM. It worked for me. – Zala Krunal Aug 01 '20 at 16:19

3 Answers3

0

See this part :

   @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    System.out.println("Remote Data" + remoteMessage);

    //Check if msg contains Data'
    if (remoteMessage.getData().size() > 0) {
        System.out.println("Message Data : " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().get("body"));
    }

    //Check if message contains notification
    if (remoteMessage.getNotification() != null) {

        System.out.println("Message Body " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
}

Reference link :

Check Here

Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
0

Deat @youteng notification in oreo must needed Channel Id inside notification builder object. So check in your code if you have added channel id or not. you can have look in below code. Also have look here

// Sets an ID for the notification, so it can be updated.
int notifyID = 1; 
String CHANNEL_ID = "my_channel";// channel id 
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_notify_status)
            .setChannelId(CHANNEL_ID)
            .build();
Jaykishan Sewak
  • 822
  • 6
  • 13
  • thank you for reply, here are what I done but not work. https://github.com/youtengli/myfcm/blob/master/MyFirebaseMessagingService.java – youteng li Mar 19 '19 at 06:16
0

There are so many details while implementing FCM. Use tools to check the sender side and receiver side. And there comes the background/foreground parts.

If your message format aren't correct, it will not trigger onReceive() when app is in background. By correct I mean it contains only data{...}, without notification{...}

Before dig into your server side's code, have you test your client side by sending a message from Firebase Console's web tool or the FCM API?

POST https://fcm.googleapis.com/fcm/send

I don't know what language you're using on server side, but the original message should look like this (from official)

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}
Wesely
  • 1,425
  • 14
  • 23
  • yes, I send by `fcm api`, I tried only `notification` or `data` and both. I can receive message when app open or onPause. By the way, I can receive message on android 7(api level 25), even app got killed. – youteng li Mar 19 '19 at 06:36