Android background notifications with Firebase Cloud Messaging not received

前端 未结 3 1044
梦谈多话
梦谈多话 2021-01-15 06:08

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 A

相关标签:
3条回答
  • 2021-01-15 06:28

    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.

    0 讨论(0)
  • 2021-01-15 06:29

    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

    0 讨论(0)
  • 2021-01-15 06:35

    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.

    public class YourApplication extends Application implements Application.ActivityLifecycleCallbacks {
        @Override
        public void onCreate() {
            super.onCreate();
            registerActivityLifecycleCallbacks(this);
        }
    
        @Override
        public void onActivityDestroyed(Activity activity) {
                Intent restartService = new Intent(getApplicationContext(), MyAppFirebaseMessagingService.class);
                PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),1,restartService,PendingIntent.FLAG_ONE_SHOT);
                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.ELAPSED_REALTIME,5000,pendingIntent);
        }
    }
    
    0 讨论(0)
提交回复
热议问题