Firebase onMessageReceived not called when app in background

前端 未结 26 2682
粉色の甜心
粉色の甜心 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:24

    Here is more clear concepts about firebase message. I found it from their support team.

    Firebase has three message types:

    Notification messages : Notification message works on background or foreground. When app is in background, Notification messages are delivered to the system tray. If the app is in the foreground, messages are handled by onMessageReceived() or didReceiveRemoteNotification callbacks. These are essentially what is referred to as Display messages.

    Data messages: On Android platform, data message can work on background and foreground. The data message will be handled by onMessageReceived(). A platform specific note here would be: On Android, the data payload can be retrieved in the Intent used to launch your activity. To elaborate, if you have "click_action":"launch_Activity_1", you can retrieve this intent through getIntent() from only Activity_1.

    Messages with both notification and data payloads: When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification. When in the foreground, your app receives a message object with both payloads available. Secondly, the click_action parameter is often used in notification payload and not in data payload. If used inside data payload, this parameter would be treated as custom key-value pair and therefore you would need to implement custom logic for it to work as intended.

    Also, I recommend you to use onMessageReceived method (see Data message) to extract the data bundle. From your logic, I checked the bundle object and haven't found expected data content. Here is a reference to a similar case which might provide more clarity.

    From server side, firebase notification should bellow format:

    Server side should send "notification" object. Lacks of "notification" object in my TargetActivity didn't getting message using getIntent().

    Correct message format is given bellow:

    {
     "data": {
      "body": "here is body",
      "title": "Title"
     },
    "notification": {
      "body": "here is body",
      "title": "Title",
      "click_action": "YOUR_ACTION"
     },
     "to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
    }
    

    Here is more clear concepts about firebase message. I found it from their support team.

    For more info visit my this thread and this thread

    0 讨论(0)
  • 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

    <receiver android:exported="true" android:name=".FirebaseBackgroundService" android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                </intent-filter>
            </receiver>
    

    Tested this solution in latest android 8.0 version. Thanks

    0 讨论(0)
提交回复
热议问题