用户通知 - 如何从 GCM 恢复丢失的 notification_id? [英] User Notifications - How to recover a lost notification_id from GCM?
问题描述
假设由于某些错误我无法存储它,所以我尝试重新注册.
这次我收到 400 错误消息,消息notification_key already exists"
.
这看起来很奇怪,尤其是与将设备注册到 GCM 相比,您可以根据需要注册多次,并始终获得状态为 200 的相同注册 ID.
现在我无法再次注册,也无法取消注册,因为我没有notification_id
.
有什么办法可以从 GCM 中获取之前生成的 notification_key
吗?
或者是使用不同的notification_key_name
重新注册的唯一方法?
Suppose that due to some error I fail to store it, so I try to re-register.
This time I receive a 400 error with the message "notification_key already exists"
.
This looks odd, especially compared to registration of a device to GCM, where you can register as many times as you want and always get the same Registration ID with status 200.
Now I can't register again, and I also can't unregister, because I don't have the notification_id
.
Is there any way to get the previously generated notification_key
from GCM?
Or is the only way to register again with a different notification_key_name
?
推荐答案
根据文档,无法从 GCM 获取现有 notification_key_name
的 notification_key
.如果您考虑一下,尝试为现有的 notification_key_name
创建一个新的 notification_key
会给您一个错误是有道理的,因为如果不是这种情况,您如果您不小心提供了现有的 notification_key_name
,可能会意外覆盖现有 notification_key
的注册 ID.
Based on the docs, there is no way to get from GCM the notification_key
of an existing notification_key_name
. If you think about it, it makes sense that trying to create a new notification_key
for an existing notification_key_name
would give you an error, since if it wasn't the case, you might be accidentally overwriting the Registration IDs of an existing notification_key
if you happen to supply an existing notification_key_name
by mistake.
您将此与多次向 GCM 注册设备进行比较,每次都获得相同的注册 ID,但这不是类似的情况.当您向 GCM 注册设备时,GCM 有一种方法可以识别设备并知道它已经注册并返回相同的注册 ID.对于用户通知,它只有您提供的 notification_key_name
,并且没有什么可以阻止您为多个用户使用相同的 notification_key_name
.也就是说,有些东西会阻止您 - 在尝试使用以前使用的 notification_key_name
创建 notification_key
时遇到的错误.
You are comparing this to registering a device to GCM multiple times, each time getting the same Registration ID, but it's not a similar situation. When you register a device to GCM, GCM has a way to identify the device and know that it is already registered and return the same Registration ID. With user notifications, it only has the notification_key_name
that you supplied, and there's nothing stopping you from using the same notification_key_name
for multiple users. That is, there is something stopping you - the error you got when trying to create a notification_key
with a previously used notification_key_name
.
解决问题的一种简单方法是将 notification_key_name
视为服务器生成的唯一标识符.如果您没有某个用户的 notification_key
(因为它是新用户,或者因为您未能存储之前从 Google 获得的 notification_key
),您将生成一个新的唯一 notification_key_name
并使用它来创建一个新的 notification_key
.您不必关心您未能存储的旧 notification_key
.
An easy way to overcome your problem is to treat notification_key_name
as a unique identifier generated by your server. If you don't have a notification_key
for a certain user (either because it's a new user or because you failed to store the notification_key
you previously got from Google), you generate a new unique notification_key_name
and use it to create a new notification_key
. You don't have to care about the old notification_key
that you failed to store.
最后,您将 notification_key
和 notification_key_name
存储在包含用户 ID 的表中.
Finally you store both the notification_key
and notification_key_name
in a table that contains the user id.
这篇关于用户通知 - 如何从 GCM 恢复丢失的 notification_id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!