在GoogleCloudMessaging API,如何处理注册ID的更新或过期的? [英] In GoogleCloudMessaging API, how to handle the renewal or expiration of registration ID?

查看:191
本文介绍了在GoogleCloudMessaging API,如何处理注册ID的更新或过期的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为这个问题说,如何找出什么时候在注册ID 已成为 GoogleCloudMessaging API无效? 我已经阅读类似主题的几个问题的答案:做GCM​​注册ID的到期?和<一href="http://stackoverflow.com/questions/12824924/google-coud-mesaging-gcm-and-registration-id-expiry-how-will-i-know?lq=1">Google Coud Mesaging(GCM)和registration_id到期,我怎么会知道?。这些问题的问题是,答案的是 C2DM 或旧GCM API,它使用的 GCMRegistrar 而不是GoogleCloudMessaging API。在previous两个方法已经去preciated。

As the question says that How to find out when does the registration ID has become invalid in GoogleCloudMessaging API? I already read the answers on few questions on similar topic: Do GCM registration id's expire? and Google Coud Mesaging (GCM) and registration_id expiry, how will I know? . The issue with those question is that the answers there are for C2DMor old GCM API which used GCMRegistrar instead of GoogleCloudMessaging API. The previous two methods have been depreciated.

我会尽力为打破我的困惑/问题逐步

1)在标题启用GCM ,在第二点,它说:

1) Under the heading Enable GCM, in the second point it says:

谷歌可能会定期刷新注册ID,所以你应该设计自己的Andr​​oid应用程序的理解是,com.google.android.c2dm.intent.REGISTRATION意图可能被多次调用。 Android应用程序需要能够作出相应的反应。

的注册ID会一直持续到Android应用程序明确地注销本身,或者直到谷歌将刷新你的Andr​​oid应用程序的注册ID。每当应用程序接收com.google.android.c2dm.intent.REGISTRATION意图与registration_id多余的,它应该保存ID以备将来使用,它传递给第三方服务器来完成注册,并跟踪是否对服务器完成注册。如果服务器未完成注册,就应该再试一次或从GCM注销。

2)现在,如果是这样的话,那么我应该处理这个意图在一个BroadcastReceiver并发送<一href="http://developer.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register%28java.lang.String...%29"又相对=nofollow>寄存器()要求得到一个新的注册ID。但问题是,在同一页面上标题下的<一个href="http://developer.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#ERROR_MAIN_THREAD"相对=nofollow> ERROR_MAIN_THREAD ,它说: GCM方法阻止。你不应该在主线程或在广播接收器运行它们。

2) Now, if that's the case then I should handle the intent in a BroadcastReceiver and send the register() request again to get a new registration ID. But the issue is that on the same page under heading ERROR_MAIN_THREAD, it says that: GCM methods are blocking. You should not run them in the main thread or in broadcast receivers.

3)我也明白,还有其他两种情况时,注册ID的变化(标题下的保持同步注册状态): 应用程序更新和备份和放大器;恢复。我已经处理他们在开放的应用程序中。

3) I also understand that there are other two scenarios when the registration ID changes( as mentioned under Advanced Topics under heading Keeping the Registration State in Sync): Application update and Backup&restore. I am already handling them on opening of the app.

4)在GCMRegistrar API,里面的 GCMBaseIntentService ,也曾经是一个回调<一个href="http://developer.android.com/reference/com/google/android/gcm/GCMBaseIntentService.html#onRegistered%28android.content.Context,%20java.lang.String%29"相对=nofollow> onRegistered()的方法,该方法得到调用的时候,设备得到了注册。在这里,我用坚持的注册ID和发送到第三方服务器。

4) In GCMRegistrar API, inside GCMBaseIntentService, there used to be a callback onRegistered() method, which got called when the device got registered. Here I used to persist the registration ID and send to 3rd party servers.

不过,现在我应该如何办理注册ID的updation或续期,坚持它,并将其发送到第三方服务器?

But, now How should I handle the updation or renewal of the registration ID, persist it and send it to 3rd party server?

这可能是要么我越来越糊涂通过阅读它的全部或我失去了一些东西。我会很感谢你的帮助。

It might be that either I am getting confused by reading all of it or I am missing something. I would be really thankful for your help.

更新

即使在<一个href="http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on-android">Handling在Android 线程在谷歌的云信息注册ID的变化,也没有提如何通过谷歌办理身份证的周期提神?

Even on Handling registration ID changes in Google Cloud Messaging on Android thread, there is no mentioning of how to handle the periodic refreshing of ID by Google?

推荐答案

我给一种方式,我在我的应用程序中实现

I am giving a way as What I implemented in my application

@Override
protected void onRegistered(Context context, String registrationId) {
     Log.i(TAG, "Device registered: regId = " + registrationId);
     //displayMessage(context, getString(R.string.gcm_registered));
     //ServerUtilities.register(context, registrationId);
     //1. Store this id to application Prefs on each request of device registration
     //2. Clear this id from app prefs on each request of device un-registration  
     //3. Now add an if check for new registartion id to server, you can write a method on server side to check if this reg-id matching for this device or not (and you need an unique identification of device to be stored on server)
     //4. That method will clear that if id is matching it meanse this is existing reg-id, and if not matching this is updated reg-id.
     //5. If this is updated reg-id, update on server and update into application prefs.
}


您可以这样做也


You can do like this also

if reg_id exists_into prefrences then
    if stored_id equals_to new_reg_id then
        do nothing
    else
        say server to reg_id updated
        update prefrences with new id
    end if
else
    update this id to application prefs
    say server that your device is registered
end if

但是,当用户清除应用程序数据,您将失去当前的REG-ID出现问题。

But problem arises when, user clears the application data and you will loose the current reg-id.

更新为新的API例如学分转到叶兰他的回答<一href="http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on-android/16839326">Handling在谷歌云消息在Android 的注册ID更改

Update for new API example Credits goes to Eran and His Answer Handling registration ID changes in Google Cloud Messaging on Android

谷歌改变了他们的<一个href="http://$c$c.google.com/p/gcm/source/browse/gcm-client/src/com/google/android/gcm/demo/app/DemoActivity.java?r=44e0469692d7d10a104cd689d3045a56971f90ce"相对=nofollow>演示应用使用新界面。他们刷新注册ID通过设置上由应用局部持久的值的有效期限。当应用程序启动时,它们加载它们的本地存储的注册ID。如果是过期,他们叫 gcm.register(senderID)了。

Google changed their Demo App to use the new interface. They refresh the registration ID by setting an expiration date on the value persisted locally by the app. When the app starts, they load their locally stored registration id. If it is "expired" (which in the demo means it was received from GCM over 7 days ago), they call gcm.register(senderID) again.

这不处理的假想方案,其中,注册ID是由谷歌对于尚未推出很长一段时间的应用程序更新。在这种情况下,该应用将不知道的改变,也不将第三方服务器

This doesn't handle the hypothetical scenario in which a registration ID is refreshed by Google for an app that hasn't been launched for a long time. In that case, the app won't be aware of the change, and neither will the 3rd party server.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mDisplay = (TextView) findViewById(R.id.display);

    context = getApplicationContext();
    regid = getRegistrationId(context);

    if (regid.length() == 0) {
        registerBackground();
    }
    gcm = GoogleCloudMessaging.getInstance(this);
}

/**
 * Gets the current registration id for application on GCM service.
 * <p>
 * If result is empty, the registration has failed.
 *
 * @return registration id, or empty string if the registration is not
 *         complete.
 */
private String getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    if (registrationId.length() == 0) {
        Log.v(TAG, "Registration not found.");
        return "";
    }
    // check if app was updated; if so, it must clear registration id to
    // avoid a race condition if GCM sends a message
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion || isRegistrationExpired()) {
        Log.v(TAG, "App version changed or registration expired.");
        return "";
    }
    return registrationId;
}

/**
 * Checks if the registration has expired.
 *
 * <p>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;
}

这篇关于在GoogleCloudMessaging API,如何处理注册ID的更新或过期的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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