【问题标题】:Firebase Cloud Messaging (FCM) onMessageReceived never gets called in the serviceFirebase 云消息传递 (FCM) onMessageReceived 永远不会在服务中被调用
【发布时间】:2018-07-23 12:27:47
【问题描述】:

我正在使用 firebase 向 Android 设备发送推送通知。当我使用notification 发送推送时,它工作正常,设备显示通知,单击通知会将用户发送到主要活动,并按预期将数据作为意图附加信息发送。这是我使用notification 发送推送通知的请求。这是我对https://fcm.googleapis.com/fcm/sendPOST 请求的正文

{
"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 部分并发出如下请求时,它不再有效,并且我的服务没有接收到我的服务中的数据。我不想要任何通知,我只想让我的服务唤醒并接收数据。

{
"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"
    }
}

请求被发送到 FCM 并得到这样的成功响应

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

MyFirebaseService 看起来像这样

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>

我不知道是什么问题,因为我没有收到任何错误。我的推送根本没有收到onMessageReceived() 方法。

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

【问题讨论】:

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


【解决方案1】:

您忘记在 MyFirebaseService 中添加 com.google.firebase.MESSAGING_EVENT&lt;intent-filter&gt;

现在我们不需要使用"com.google.firebase.INSTANCE_ID_EVENT&lt;intent-filter&gt;,因为FirebaseInstanceIdService已被弃用

示例代码

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

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 2018-08-04
    • 2016-10-09
    • 2021-03-31
    • 2022-01-09
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多