Firebase onMessageReceived not called when app in background

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

    I had this issue(app doesn't want to open on notification click if app is in background or closed), and the problem was an invalid click_action in notification body, try removing or changing it to something valid.

    0 讨论(0)
  • 2020-11-22 03:05

    There are 2 types of Firebase push-notifications:

    1- Notification message(Display message) -> -- 1.1 If you choose this variant, the OS will create it self a notification if app is in Background and will pass the data in the intent. Then it's up to the client to handle this data.

    -- 1.2 If the app is in Foreground then the notification it will be received via callback-function in the FirebaseMessagingService and it's up to the client to handle it.

    2- Data messages(up to 4k data) -> These messages are used to send only data to the client(silently) and it's up to the client to handle it for both cases background/foreground via callback-function in FirebaseMessagingService

    This is according official docs:https://firebase.google.com/docs/cloud-messaging/concept-options

    0 讨论(0)
  • 2020-11-22 03:07

    By default the Launcher Activity in you app will be launched when your app is in background and you click the notification, if you have any data part with your notifcation you can handle it in the same activity as follows,

    if(getIntent().getExtras()! = null){
      //do your stuff
    }else{
      //do that you normally do
    }
    
    0 讨论(0)
  • 2020-11-22 03:07

    If app is in background Fire-base by default handling notification But if we want to our custom notification than we have to change our server side, which is responsible for to send our custom data(data payload)

    Remove notification payload completely from your server request. Send only Data and handle it in onMessageReceived() otherwise your onMessageReceived will not be triggered when app is in background or killed.

    now,your server side code format look like,

    {
      "collapse_key": "CHAT_MESSAGE_CONTACT",
      "data": {
        "loc_key": "CHAT_MESSAGE_CONTACT",
        "loc_args": ["John Doe", "Contact Exchange"],
        "text": "John Doe shared a contact in the group Contact Exchange",
        "custom": {
          "chat_id": 241233,
          "msg_id": 123
        },
        "badge": 1,
        "sound": "sound1.mp3",
        "mute": true
      }
    }
    

    NOTE: see this line in above code
    "text": "John Doe shared a contact in the group Contact Exchange" in Data payload you should use "text" parameter instead of "body" or "message" parameters for message description or whatever you want to use text.

    onMessageReceived()

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.e(TAG, "From: " + remoteMessage.getData().toString());
    
            if (remoteMessage == null)
                return;
    
            // Check if message contains a data payload.
            if (remoteMessage.getData().size() > 0) {
               /* Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());*/
                Log.e(TAG, "Data Payload: " + remoteMessage);
    
                try {
    
                    Map<String, String> params = remoteMessage.getData();
                    JSONObject json = new JSONObject(params);
                    Log.e("JSON_OBJECT", json.toString());
    
    
                    Log.e(TAG, "onMessageReceived: " + json.toString());
    
                    handleDataMessage(json);
                } catch (Exception e) {
                    Log.e(TAG, "Exception: " + e.getMessage());
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 03:07

    Try this:

    public void handleIntent(Intent intent) {
        try {
            if (intent.getExtras() != null) {
                RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
                for (String key : intent.getExtras().keySet()) {
                builder.addData(key, intent.getExtras().get(key).toString());
            }
                onMessageReceived(builder.build());
            } else {
                super.handleIntent(intent);
            }
        } catch (Exception e) {
            super.handleIntent(intent);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:08

    As per Firebase Cloud Messaging documentation-If Activity is in foreground then onMessageReceived will get called. If Activity is in background or closed then notification message is shown in the notification center for app launcher activity. You can call your customized activity on click of notification if your app is in background by calling rest service api for firebase messaging as:

    URL-https://fcm.googleapis.com/fcm/send

    Method Type- POST

    Header- Content-Type:application/json
    Authorization:key=your api key
    

    Body/Payload:

    { "notification": {
        "title": "Your Title",
        "text": "Your Text",
         "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
      },
        "data": {
        "keyname": "any value " //you can get this data as extras in your activity and this data is optional
        },
      "to" : "to_id(firebase refreshedToken)"
    } 
    

    And with this in your app you can add below code in your activity to be called:

    <intent-filter>
                    <action android:name="OPEN_ACTIVITY_1" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    
    0 讨论(0)
提交回复
热议问题