Firebase onMessageReceived not called when app is in the background

后端 未结 4 1811
感情败类
感情败类 2020-11-28 11:38

I am using Firebase for our messaging service in our Android app. I have researched Firebase quite a bit and I understand that whether the app if running in the foreground o

相关标签:
4条回答
  • 2020-11-28 11:56

    In this post , answer of Koot might be your solution. He said "I had the same problem. It is easier to use the 'data message' instead of the 'notification'. The data message always load the class onMessageReceived."

    my solution is as per his advice

    {
      "condition": "'reader' in topics",
    
      "data": {
        "title":"Please Check",
        "message": "This is a Firebase Cloud Messaging Topic Message 5!"
       }
    }
    
    0 讨论(0)
  • 2020-11-28 12:05

    Pretty sure, you already know the behavior in Android that when using both notification and data payload, the Android system catches what's in the notification and the data payload is included in the intent when it's tapped.

    AFAIK, this is the only way that it goes (as per mentioned by @ArthurThompson here) and if you really want to make sure that onMessageReceived() is always called, you have to only use data payload (as mentioned by @DiegoGiorgini here) However, I've looked around, maybe there's another way.

    I haven't tried it out myself, but have you considered checking which notifications are in the Status Bar, then if your notification exists (not entirely sure if you can actually identify which one is a notification for your app though), call the methods you usually call inside onMessageReceived().

    One approach that you can also do is to detect if the device(s) you'll be sending the message to is an Android device in you app server, then send only a data payload.

    0 讨论(0)
  • 2020-11-28 12:09

    Guyz Listen carefully, I gone through many links and finally found solution here.

    1. When app is background onMessageReceived method should call

    This payload will works...

    {
      "data":{
        "title": "hello",
        "body": "this is body"
      },
      "to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
    }
    

    This payload will not work...

    {
      "notification":{
        "title": "hello",
        "body": "this is body"
      },
      "to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
    }
    

    Because when the load contains "notification" onMessageReceived will not call

    0 讨论(0)
  • 2020-11-28 12:12

    Remove the notification payload from your FCM messages in order to have the data payload delivered to the onMessageReceived method.

    Read this and this carefully.

    When your app is in the background, data payload delivered to the onMessageReceived method only if there is no notification payload. (Mark the words)

    In case both payloads exist then system automatically handles the notification part (system tray) and your app gets the data payload in the extras of the intent of launcher Activity (after the user tap on the notification).

    In order to be able to serve both platforms successfully, Android and iOS, you may have to send different FCM messages according to client's OS.

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