Firebase onMessageReceived not called when app in background

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

    Remove notification field 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.

    Don't forget to include "priority": "high" field in your notification request. According to the documentation: data messages are sent with a normal priority, thus they will not arrive instantly; it could also be the problem.

    Here is what I am sending from server

    {
      "data":{
        "id": 1,
        "missedRequests": 5
        "addAnyDataHere": 123
      },
      "to": "fhiT7evmZk8:APA91bFJq7Tkly4BtLRXdYvqHno2vHCRkzpJT8QZy0TlIGs......",
      "priority": "high"
    }
    

    So you can receive your data in onMessageReceived(RemoteMessage message) like this....let say I have to get id

    Object obj = message.getData().get("id");
            if (obj != null) {
                int id = Integer.valueOf(obj.toString());
            }
    

提交回复
热议问题