1

我正在使用 firebase 向 Android 设备发送推送通知。当我发送推送时notification它工作正常,设备会显示通知,单击通知会将用户发送到主要活动,并按预期将数据作为意图附加信息发送。这是我发送推送通知的请求notification。这是我POST请求的正文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部分并发出如下请求时,它不再有效,并且我的服务没有接收到我的服务中的数据。我不想要任何通知,我只想让我的服务唤醒并接收数据。

{
"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()方法。

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

4

1 回答 1

1

你忘了<intent-filter>com.google.firebase.MESSAGING_EVENT你的MyFirebaseService

现在我们不需要使用 <intent-filter>因为"com.google.firebase.INSTANCE_ID_EVENTFirebaseInstanceIdService弃用

示例代码

<service android:name=".fcm.MyFirebaseService">
   <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
 </service>
于 2018-07-23T12:28:42.683 回答