FireBase Cloud Messaging(FCM)onMessageReceived永远不会在服务中被调用

时间:2018-07-23 12:27:47

标签: android firebase push-notification firebase-cloud-messaging android-push-notification

我正在使用Firebase向Android设备发送推送通知。当我使用notification发送推送时,它会正常工作,设备会显示一条通知,然后单击该通知将把用户转到主要活动,并按预期发送数据作为意向附加信息。这是我的请求,用于通过notification发送推送通知。这是我对POST的{​​{1}}请求的正文

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

上面的代码有效,但是当我删除{ "to" : "d_axTaq9LxM:APA91bH3Q-u0TEEWTTuihv-UKpEHy_J00gMsYdCvnRUdqaa1tN2VdI0AUyE0c8yOvFnZF6XQ4cORbUsTGe84YGsS4xkgDOI7tOE33eJUT8OYdFYRkSfeFntcs3zcd54BObcXxwAF1VFlK4gL1ByICkHnjXXmaiShZA", "collapse_key" : "type_a", "notification" : { "body" : "First Notification", "title": "Collapsing A" }, "data" : { "body" : "First Notification", "title": "ALT App Testing", "key_1" : "Data for key one", "key_2" : "Hellowww" } } 部分并发出如下所示的请求时,它不再起作用,并且我的服务未收到服务中的数据。我不需要任何通知,只希望我的服务唤醒并接收数据。

notification

该请求已发送到FCM,并获得了这样的成功响应

{
"to" : "d_axTaq9LxM:APA91bH3Q-u0TEEWTTuihv-UKpEHy_J00gMsYdCvnRUdqaa1tN2VdI0AUyE0c8yOvFnZF6XQ4cORbUsTGe84YGsS4xkgDOI7tOE33eJUT8OYdFYRkSfeFntcs3zcd54BObcXxwAF1VFlK4gL1ByICkHnjXXmaiShZA",
    "collapse_key" : "type_a",
    "data" : {
        "body" : "First Notification",
        "title": "ALT App Testing",
        "key_1" : "Data for key one",
        "key_2" : "Hellowww"
    }
}

MyFirebaseService看起来像这样

{
    "multicast_id": 6489464663382515471,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1532347638201363%5a92300dcccfb49c"
        }
    ]
}

这是清单文件中我的服务的定义

public class MyFirebaseService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Push notification message data payload: " + 
remoteMessage.getData());
        }
    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);

        Log.e("alz", "new firebase token: " + s);
        Toast.makeText(this, "new firebase toekn: " + s, Toast.LENGTH_SHORT).show();
    }
}

我没弄清楚是什么问题,因为我没有得到任何错误。我的推送根本无法通过 <service android:name=".fcm.MyFirebaseService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> 方法接收。

任何帮助或建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

您忘记在<intent-filter>

中添加 com.google.firebase.MESSAGING_EVENT 中的MyFirebaseService
  

现在我们不需要使用<intent-filter>中的 "com.google.firebase.INSTANCE_ID_EVENT ,因为 FirebaseInstanceIdService 已过时

示例代码

<service android:name=".fcm.MyFirebaseService">
   <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
 </service>