Android background notifications with Firebase Cloud Messaging not received

前端 未结 3 1070
梦谈多话
梦谈多话 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: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);
        }
    }
    

提交回复
热议问题