[英]how to send push notification to the android device using GCM by java server?
[英]How to get registration id of failure push notification on android device using Java GCM API
我已经使用GCM API在Java中实现了以下代码,以在Android设备上发送通知。
Sender sender = new Sender(GCM_API_KEY);
Message.Builder builder = new Message.Builder();
builder.collapseKey(collpaseKey);
builder.timeToLive(TTL_HOURS * 3600);
if (data != null) {
for (PushData messageData : data) {
builder.addData(messageData.getKey(), messageData.getValue());
}
}
if (StreamType.CALL == type) {
for (PushData pushData : message) {
builder.addData(pushData.getKey(), pushData.getValue());
}
}
Message msg = builder.build();
try {
System.out.println(msg.toString());
MulticastResult result = sender.send(msg, pushId, MAX_RETRY);
log.infof("result %s", result.toString());
return true;
}
这里pushId是列表的类型,其中包含所有设备的推送ID,在该设备上将发送通知。
问题是,结果不包含故障推送ID或任何设备标识,因此我可以标识哪个设备尚未收到通知。 注意:-我不想一一发送通知
回应-结果
MulticastResult(multicast_id = 7588781423174816193,总计= 5,成功= 1,失败= 4,canonical_ids = 0,结果:[[errorCode = NotRegistered],[messageId = 0:1505731620878025%f02bfbe47eeff668],[errorCode = NotRegistered],[errorCode =未注册],[errorCode =未注册]]
它不包含任何注册ID或什么,以便我可以在db中识别设备。
请提出建议,在此先感谢。
响应会间接告诉您推送ID失败。
在您的示例中:
[[ errorCode=NotRegistered ],
[ messageId=0:1505731620878025%f02bfbe47eeff668 ],
[ errorCode=NotRegistered ],
[ errorCode=NotRegistered ],
[ errorCode=NotRegistered ]]
您会看到第1,第3,第4和第5个推送ID失败。 由于您应该知道在请求中按哪个顺序发送了推送ID,因此您应该知道哪个推送ID失败。
这是文档中的一个相关示例:
以下是6个接收者(分别为ID 4、8、15、16、23和42)的JSON结果,其中3个消息已成功处理,返回了1个规范的注册令牌,以及3个错误:
{“ multicast_id”:216,“ success”:3,“ failure”:3,“ canonical_ids”:1,“ results”:[
{“ message_id”:“ 1:0408”},
{“错误”:“不可用”},
{“ error”:“ InvalidRegistration”},
{“ message_id”:“ 1:1516”},
{“ message_id”:“ 1:2342”,“ registration_id”:“ 32”},
{“ error”:“ NotRegistered”}]}在此示例中:
- 第一条消息:成功,不是必需的。
- 第二条消息:应重新发送(至注册令牌8)。
- 第三条消息:发生了不可恢复的错误(可能值已在数据库中损坏)。
- 第四个讯息:成功,什么都不需要。
- 第五条消息:成功,但是注册令牌应该在服务器数据库中更新(从23到32)。
- 第六条消息:应从服务器数据库中删除注册令牌(42),因为已从设备上卸载了应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.