0

Firebase onMessageReceived never called when app is not running and removed from recent apps list. I'm working with Firebase and sending data payload notifications to my app from my server, i have tried to use JobScheduler to start MyFirebaseMessagingService every 1 minute in case the system is killing my service but this approach didn't work for me on One Plus 3. I know that android add limitation on background services to optimize battery usage, but does this limitation affect on FCM service?

here is my message string:

{
"notification": {
    "title": "Hello",
    "body": "Notification",
    "badge": 1,
    "icon": "app_icon",
    "color": "#ffffff",
    "sound": "default",
    "clickAction": "home",
    "tag": "parent_ccid_6"
},
"message": {
    "priority": "high",
    "type": "daily_tips",
    "data": {
        "parent_ccid": "parent_ccid_6",
        "id": "2",
        "sound": "default"
    }
},

}

and here is my Service in manifest

 <service
        android:name=".util.FCM.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
Omar Qadomi
  • 43
  • 1
  • 7

4 Answers4

0

You must use Notification Channel for above Nougat

Sample

        int notifyID = 1;
        String CHANNEL_ID = "my_channel_01";
         // The id of the channel.
        CharSequence name = "channel ram";
         // The user-visible name of the channel.
        String groupId = "Building654651";
        CharSequence groupName = "BuilgingHub123";
        int importance = 0;
            importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
 Notification notification = new Notification.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(messageBody)
                .setSmallIcon(R.drawable.ic_notifi).setColor(Color.parseColor("#FFA23B"))
                .setChannelId(CHANNEL_ID)
                .build();
  NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
        mNotificationManager.createNotificationChannel(mChannel);
            mNotificationManager.notify(notifyID, notification);
Ramkumar.M
  • 681
  • 6
  • 21
  • Thank you M.Ram, I have already implement Channels for android > N, and it works fine when application in foreground or in recent apps list, but when close the app on onMessageReceived is never triggered. – Omar Qadomi Mar 13 '18 at 10:28
0

When the application is in background onMessageReceived will never be called as per the android document. Link:- https://firebase.google.com/docs/cloud-messaging/android/receive

When app is in background the notification is delivered to the device’s system tray and default intent of your application (i.e Launcher Screen) will be called and you have to handle that notifcation on this screen.

Other solution is that you can remove the "notification" keyword from payload then you will able to receive the notification when your application is in background and onMessageReceived will get called.

kishna.147
  • 167
  • 8
  • Thank you Kishna, I have tried this solution using https://fcm.googleapis.com/fcm/send and set Post body as: `{ "to" : "ewHHsQa3R6g:APA91bF....", "priority": "high", "android_channel_id":"DEFAULT_CHANNEL", "data": { "type": "daily_tips", "daily_tips_type": "interest", "parent_ccid": "parent_ccid_6", "ccid": "ccid_6", "id": "2", "cid":1, "sound": "default" } }` but it didn't work too. – Omar Qadomi Mar 13 '18 at 12:17
  • Can you post your server notification code and android code – kishna.147 Mar 13 '18 at 12:53
  • I don't have access to server code but the message data will come as the posted question above, and it works fine with other devices even though they have android 8 and 8.1, except my one plus and Huawei. and if i go to settings and select "Don't optimize" from Battery optimization settings it works fine in background – Omar Qadomi Mar 13 '18 at 17:02
0

I didn't find any solution to solve this issue by code. The only way to revive notification is to go to settings and select "Don't optimize" from Battery optimization settings, and it works fine in background, otherwise notification handler will not be triggered. I faced this issue with all installed applications on One plus 3.

Omar Qadomi
  • 43
  • 1
  • 7
0

Try android:exported="true". in your android manifest file.

  • Could you please expand upon your answer with a little more detail. Adding context to your answers make them more valuable. – Dan Jan 27 '20 at 16:34
  • Note that there is already an accepted answer to this question. Please [edit] your answer to ensure that it improves upon other answers already present in this question. – hongsy Jan 27 '20 at 16:46