应用程序是否应该每 7 天调用一次 gcm.register() 以确保有效的注册 ID? [英] Should applications call gcm.register() every seven days to ensure valid registration IDs?

查看:16
本文介绍了应用程序是否应该每 7 天调用一次 gcm.register() 以确保有效的注册 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 开发站点上的 GCM 示例中的客户端代码 默认为每 7 天调用一次 gcm.register(SENDER_ID);,方法是使用以下函数检查注册是否已过期:

The client-side code in the GCM example on the Android dev site defaults to calling gcm.register(SENDER_ID); after every seven days by checking if registration has expired using the following function:

public static final long REGISTRATION_EXPIRY_TIME_MS = 1000 * 3600 * 24 * 7;

/**
 * Checks if the registration has expired.
 *
 * To avoid the scenario where the device sends the registration to the
 * server but the server loses it, the app developer may choose to re-register
 * after REGISTRATION_EXPIRY_TIME_MS.
 *
 * @return true if the registration has expired.
 */
private boolean isRegistrationExpired() {
    final SharedPreferences prefs = getGCMPreferences(context);
    // checks if the information is not stale
    long expirationTime =
            prefs.getLong(PROPERTY_ON_SERVER_EXPIRATION_TIME, -1);
    return System.currentTimeMillis() > expirationTime;
}

函数上方的注释暗示这用于避免设备将注册发送到服务器但服务器丢失它的情况.这是否表明我们的服务器(不是 GCM服务器)可能会丢失注册 ID?或者这是因为注册 ID 在 GCM 方面可能变得无效?根据 GCM 高级主题页面:

The comment above the function implies that this is used to "avoid the scenario where the device sends the registration to the server but the server loses it. Is this suggesting that our servers (not the GCM servers) may lose the registration id? Or is this because the registration ID could become invalid on the GCM side of things? It appears that this is possible as per the following paragraph in the GCM Advanced Topics Page:

同样,在申请时不应该保存注册ID已备份.这是因为注册 ID 可能无效到应用程序恢复时,这将使处于无效状态的应用程序(即,应用程序认为它是已注册,但服务器和 CM 不存储该注册 ID——因此应用程序将不会收到更多消息).

Similarly, you should not save the registration ID when an application is backed up. This is because the registration ID could become invalid by the time the application is restored, which would put the application in an invalid state (that is, the application thinks it is registered, but the server and CM do not store that registration ID anymore—thus the application will not get more messages).

先谢谢你!

推荐答案

  1. 你说:

函数上方的注释暗示这用于避免设备发送注册到服务器,但服务器丢失了它.这是暗示我们的服务器(不是 GCM 服务器)可能会丢失注册号?或者这是因为注册 ID 可能会变成在 GCM 方面无效?

The comment above the function implies that this is used to "avoid the scenario where the device sends the registration to the server but the server loses it. Is this suggesting that our servers (not the GCM servers) may lose the registration id? Or is this because the registration ID could become invalid on the GCM side of things?

我认为他们在谈论我们的服务器(第 3 方服务器)而不是 GCM 服务器.第二点会更清楚一点.

I think they are talking about our servers( the 3rd party servers) and NOT GCM servers. The second point will clear that a bit more.

另外,你提到文档说:

备份应用程序时不应保存注册 ID.这是因为注册 ID 可能会在应用程序已恢复,这会将应用程序置于无效状态.

You should not save the registration ID when an application is backed up. This is because the registration ID could become invalid by the time the application is restored, which would put the application in an invalid state.

我想如果你仔细阅读标题下的第二点在架构概览页面上启用 GCM,它说:

I think If you carefully read the second point under the heading Enable GCM on Architectural Overview page, it says:

请注意,Google 可能会定期刷新注册 ID,因此你应该在理解的情况下设计你的 Android 应用程序com.google.android.c2dm.intent.REGISTRATION 意图可能是多次调用.您的 Android 应用程序需要能够做出相应的回应.

Note that Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

因此,Google 可能会定期刷新注册 ID.这就是为什么注册 ID 可能会在申请时失效恢复了.

So, Google may refresh he registration ID's periodically. That's why registration ID could become invalid by the time the application is restored.

因此,为了处理该问题,您应该拥有一个可以处理 com.google.android.c2dm.intent.REGISTRATION 意图的广播侦听器,Google 在必须刷新注册时将其发送给应用程序ID.

So, for handling that you should have a Broadcast Listener which could handle com.google.android.c2dm.intent.REGISTRATION intent, which Google send to the app when it has to refresh the registration ID.

这也可能清除第一点.由于本案例是处理谷歌端的ID刷新,本地7天有效期将处理第三部分服务器丢失ID的另一种情况(因为它每7天定期刷新一次).

This also might clear the first point. As this case is handling the refreshing of the ID from Google side, the local 7 days validity will handle the other case of loosing the ID on 3rd part server( as it is being refreshed periodically after every 7 days).

这是我对你的问题的看法.希望这可以帮助.

This is my view about your question. Hope this helps.

这篇关于应用程序是否应该每 7 天调用一次 gcm.register() 以确保有效的注册 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆