Firebase Cloud Messaging (FCM) onMessageReceived 永远不会在服务中被调用
Firebase Cloud Messaging (FCM) onMessageReceived never gets called in the service
我正在使用 firebase 向 Android 设备发送推送通知。当我使用 notification
发送推送时它工作正常,设备显示通知并且单击通知会将用户发送到主 activity 并按预期将数据作为 intent extras 发送。这是我用 notification
发送推送通知的请求。这是我对 https://fcm.googleapis.com/fcm/send
的 POST
请求的正文
{
"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()
方法。
任何帮助或建议将不胜感激。
您忘记在 MyFirebaseService
中添加 <intent-filter>
共 com.google.firebase.MESSAGING_EVENT
Now we no need to use <intent-filter>
of "com.google.firebase.INSTANCE_ID_EVENT
Because FirebaseInstanceIdService
is deprecated
示例代码
<service android:name=".fcm.MyFirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我正在使用 firebase 向 Android 设备发送推送通知。当我使用 notification
发送推送时它工作正常,设备显示通知并且单击通知会将用户发送到主 activity 并按预期将数据作为 intent extras 发送。这是我用 notification
发送推送通知的请求。这是我对 https://fcm.googleapis.com/fcm/send
POST
请求的正文
{
"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()
方法。
任何帮助或建议将不胜感激。
您忘记在 MyFirebaseService
<intent-filter>
共 com.google.firebase.MESSAGING_EVENT
Now we no need to use
<intent-filter>
of"com.google.firebase.INSTANCE_ID_EVENT
BecauseFirebaseInstanceIdService
is deprecated
示例代码
<service android:name=".fcm.MyFirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>