Android Background Notifications With Firebase Cloud Messaging Not Received
I've searched a lot about notifications when the app is in the background or closed. I'm using Firebase Cloud Messaging by the way. It won't work for me. I've used the Android setu
Solution 1:
When the app is closed, it shutdowns the service. You must to restart the service.
On your Application class, implements ActivityLifecycleCallbacks and on onActivityDestroyed restart the service with an alarm.
publicclassYourApplicationextendsApplicationimplementsApplication.ActivityLifecycleCallbacks {
@OverridepublicvoidonCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@OverridepublicvoidonActivityDestroyed(Activity activity) {
IntentrestartService=newIntent(getApplicationContext(), MyAppFirebaseMessagingService.class);
PendingIntentpendingIntent= PendingIntent.getService(getApplicationContext(),1,restartService,PendingIntent.FLAG_ONE_SHOT);
AlarmManageralarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME,5000,pendingIntent);
}
}
Solution 2:
I also had issues with devices not receiving the notifications when they were closed, like they would be after a restart.
Turned out, it wasn't working with a DEBUG version of the solution, so that had to be tested in RELEASE MODE. For those using Android Studio, press the Green Play Button next to the debug button.
Solution 3:
Firebase has different types of notifications, and each has special handling.
- Assuming you're using a data push, you don't need special handling or a WakefulBroadcastReceiver.
- If you're using a notification push, the notification will appear automatically in the system tray. You cannot do any special handling there.
Check the official documents here: https://firebase.google.com/docs/cloud-messaging/android/receive
Post a Comment for "Android Background Notifications With Firebase Cloud Messaging Not Received"