Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在GoogleCloudMessaging API中,如何处理注册ID的续订或过期?_Java_Android_Google Cloud Messaging - Fatal编程技术网

Java 在GoogleCloudMessaging API中,如何处理注册ID的续订或过期?

Java 在GoogleCloudMessaging API中,如何处理注册ID的续订或过期?,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,正如问题所说,如何找出API中的何时失效? 我已经阅读了关于类似主题的几个问题的答案:和。这些问题的问题在于,答案是针对C2DM或旧的GCM-API的,后者使用的是GoogleCloudMessaging-API。前两种方法已折旧 我将尝试逐步打破我的困惑/问题: 1)标题下的第二点是: Google可能会定期刷新注册ID,因此您在设计Android应用程序时应了解com.Google.Android.c2dm.intent.registration intent可能会被多次调用。您的Andro

正如问题所说,如何找出API中的何时失效? 我已经阅读了关于类似主题的几个问题的答案:和。这些问题的问题在于,答案是针对
C2DM
或旧的GCM-API的,后者使用的是GoogleCloudMessaging-API。前两种方法已折旧

我将尝试逐步打破我的困惑/问题:

1)标题下的第二点是:

Google可能会定期刷新注册ID,因此您在设计Android应用程序时应了解com.Google.Android.c2dm.intent.registration intent可能会被多次调用。您的Android应用程序需要能够做出相应的响应。

注册ID将持续到Android应用程序显式注销自身,或者直到Google刷新您的Android应用程序的注册ID。每当应用程序收到带有额外注册id的com.google.android.c2dm.intent.REGISTRATION intent时,它都应保存该id以供将来使用,将其传递给第三方服务器以完成注册,并跟踪服务器是否完成注册。如果服务器未能完成注册,则应重试或从GCM注销。

2)现在,如果是这样的话,我应该在BroadcastReceiver中处理意图,并再次发送请求以获取新的注册ID。但问题是,在标题下的同一页上,它说:
GCM方法正在阻塞。您不应该在主线程或广播接收器中运行它们

3)我还了解到当注册ID发生变化时还有另外两种情况(如标题“高级主题”下所述): 应用程序更新、备份和恢复。我已经在打开应用程序时处理它们了

4)在gcmregistarapi内部,曾经有一个回调方法,在设备注册时调用该方法。在这里,我用来保存注册ID并发送到第三方服务器

但是,现在我应该如何处理注册ID的更新或续订,保存它并将其发送到第三方服务器

这可能是因为我读了所有的内容而感到困惑,或者我遗漏了一些东西。我真的很感谢你的帮助

更新

即使在线程中,也没有提到如何处理谷歌定期刷新ID

我正在为我在应用程序中实现的东西让路

@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.
}

你也可以这样做

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
但当用户清除应用程序数据时,问题就会出现,您将丢失当前的注册id


新API示例的更新学分转到和他的答案

谷歌改变了他们的习惯,使用新的界面。他们通过设置应用程序在本地保留的值的过期日期来刷新注册ID。当应用程序启动时,他们加载本地存储的注册id。如果注册id“过期”(在演示中,这意味着它是7天前从GCM收到的),他们将再次调用
GCM.register(senderID)

这不适用于假设的场景,即谷歌为一个很久没有启动的应用程序刷新注册ID。在这种情况下,应用程序不会意识到更改,第三方服务器也不会

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;
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDisplay=(TextView)findViewById(R.id.display);
context=getApplicationContext();
regid=getRegistrationId(上下文);
如果(regid.length()==0){
寄存器background();
}
gcm=GoogleCloudMessaging.getInstance(this);
}
/**
*获取GCM服务上应用程序的当前注册id。
*
*如果结果为空,则表示注册失败。
*
*@返回注册id,如果未注册,则返回空字符串
*完成。
*/
私有字符串getRegistrationId(上下文){
最终SharedReferences prefs=getGCMPreferences(上下文);
String registrationId=prefs.getString(PROPERTY_REG_ID,“”);
if(registrationId.length()=0){
Log.v(标签“未找到注册”);
返回“”;
}
//检查应用程序是否已更新;如果已更新,则必须将注册id清除为
//如果GCM发送消息,则避免竞争条件
int registeredVersion=prefs.getInt(属性\应用\版本,整数.MIN \值);
int currentVersion=getAppVersion(上下文);
if(registeredVersion!=currentVersion | | isRegistrationExpired()){
Log.v(标签“应用程序版本更改或注册过期”);
返回“”;
}
返回注册ID;
}
/**
*检查注册是否已过期。
*
*以避免设备将注册发送到
*如果服务器丢失,应用程序开发人员可以选择重新注册
*注册期满后的时间。
*
*@如果注册已过期,则返回true。
*/
私有布尔值isRegistrationExpired(){
最终SharedReferences prefs=getGCMPreferences(上下文);
//检查信息是否过时
长失效时间=
prefs.getLong(服务器上的属性\u过期\u时间,-1);
return System.currentTimeMillis()>expirationTime;
}

只是为了补充Pankaj的答案:

  • 这(Google的入门文档示例)无法处理注册ID为
    谷歌刷新了一个很久没有发布的应用程序
    时间在这种情况下,应用程序将不会意识到更改,也不会意识到
    将删除第三方服务器。

    文档上的示例确实如此