FirebaseMessagingService is not working in background using one plus 3 android oreo 8.0

4.7k Views Asked by At

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>
4

There are 4 best solutions below

0
Omar Qadomi On BEST ANSWER

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.

1
Ramkumar.M On

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);
3
kishna.147 On

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.

2
AudioBubble On

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