Firebase onMessageReceived not called when app in background

前端 未结 26 2659
粉色の甜心
粉色の甜心 2020-11-22 02:32

I\'m working with Firebase and testing sending notifications to my app from my server while the app is in the background. The notification is sent successfully, it even appe

26条回答
  •  孤独总比滥情好
    2020-11-22 03:25

    I got the same issue. If the app is foreground - it triggers my background service where I can update my database based on the notification type. But, the app goes to the background - the default notification service will be taken care to show the notification to the user.

    Here is my solution to identify app in background and trigger your background service,

    public class FirebaseBackgroundService extends WakefulBroadcastReceiver {
    
      private static final String TAG = "FirebaseService";
    
      @Override
      public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "I'm in!!!");
    
        if (intent.getExtras() != null) {
          for (String key : intent.getExtras().keySet()) {
            Object value = intent.getExtras().get(key);
            Log.e("FirebaseDataReceiver", "Key: " + key + " Value: " + value);
            if(key.equalsIgnoreCase("gcm.notification.body") && value != null) {
              Bundle bundle = new Bundle();
              Intent backgroundIntent = new Intent(context, BackgroundSyncJobService.class);
              bundle.putString("push_message", value + "");
              backgroundIntent.putExtras(bundle);
              context.startService(backgroundIntent);
            }
          }
        }
      }
    }
    

    In the manifest.xml

    
                
                    
                
            
    

    Tested this solution in latest android 8.0 version. Thanks

提交回复
热议问题