gpt4 book ai didi

android - GCM 注册设备接收两种类型的通知

转载 作者:行者123 更新时间:2023-11-29 19:55:40 25 4
gpt4 key购买 nike

我使用 example project 注册了我的安卓设备在谷歌的开发者网站上,只按预期将配置文件添加到项目中。现在,当我使用 node-gcm 从我的 nodeJS 服务器发送通知时,我收到两种类型的通知: Notification screenshot
如果我运行 ./gradlew run -Pmsg="<message>",通知“GCM 消息”是应用程序默认获取的内容终端中的命令来测试它,但它不是空白的。另一个通知“新聊天消息”是我打算发送的。
我的问题是我只想接收非空白的默认通知。我该如何解决?

nodeJS 服务器代码:

message.addNotification('title', 'New Chat Message');
message.addNotification('icon', 'ic_launcher');
message.addNotification('message', 'TEST MESSAGE');
message.addNotification('body', name + ": " + msg);
sender.send(message, {registrationTokens: regTokens}, function (err, response) {
if (err) console.error(err);
else console.log(response);
});

处理消息的Android APP代码:

public void onMessageReceived(String from, Bundle data) {
System.out.println("MyGcmListenerService.onMessageReceived");
String message = data.getString("message");
if(data.containsKey("body")){
System.out.println("Contains");
}
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);

if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
sendNotification(message);
}

EDIT- 在照片中,我两次收到“why man”消息,因为我发送了两次。这没什么不对的。

另一个编辑- 我刚刚注意到,当我的设备休眠或应用程序暂停或终止时,我才收到使用 nodeJS 服务器的第二种类型的通知。但是如果我运行 gradlew在上述任何情况下执行命令,一切正常。

最佳答案

我修好了!问题出在服务器端,我应该使用 message.addData() 而不是 message.addNotification()

关于android - GCM 注册设备接收两种类型的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36753415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com