【发布时间】:2017-05-23 12:30:18
【问题描述】:
我正在尝试通过 FCM 服务发送推送通知。 在我的 MainActivity 中,我编写了以下代码:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
}
}
};
为了给用户订阅一个主题。
我从后端通过此链接调用 FCM 服务:https://fcm.googleapis.com/fcm/send
我在其中传递了这个 JSON:
{
"to":"/topics/global",
"data":{
"title":"test",
"is_background":false,
"message":"testmessage",
"image":"",
"payload":{
"team":"Test",
"score":"5.6"
},
"timestamp":"2017-05-23 11:55:35"
}
}
我得到了这样的回应:
{\"message_id\":8863205901902209389}
但是我的设备没有显示任何通知,除非我将 Firebase 控制台与 "user segment" 或 "single device" 一起使用。在 Firebase 控制台中也不能以“主题”方式工作。
提前感谢您的任何回复。
【问题讨论】:
-
这是您发送的完整 JSON 字符串吗? "to":"/topics/topicName" 属性在哪里?您不应该指定要将消息发送到哪个主题吗?
-
嗨 Slaiv206。那是您正在使用的完整有效负载吗?我没有看到
to参数。 -
我编辑了问题,无论如何我指定了主题但它不起作用
-
你希望这个payload做什么?它不包含“通知”字典,因此 Android 不会自动显示任何内容。您必须自己处理“数据”字典。
-
发送
data-only 消息负载时,您应该自己处理负载。见Handling Messages in Android。
标签: android firebase google-cloud-messaging firebase-cloud-messaging