Android GCM:服务器端的多个应用程序ID

Android GCM:服务器端的多个应用程序ID,android,Android,在服务器端,我发现一台设备有多个注册ID,这显然给我带来了很多问题。喜欢多次收到的信息 我如何获得旧注册ID的红色信息,或者如果存在有效的注册ID,如何确保注册不会发生 我在编写应用程序时遵循了Android doc的示例教程,如下所示: checkNotNull(SERVER_URL, "SERVER_URL"); checkNotNull(SENDER_ID, "SENDER_ID"); // Make sure the device has the prop

在服务器端,我发现一台设备有多个注册ID,这显然给我带来了很多问题。喜欢多次收到的信息

我如何获得旧注册ID的红色信息,或者如果存在有效的注册ID,如何确保注册不会发生

我在编写应用程序时遵循了Android doc的示例教程,如下所示:

checkNotNull(SERVER_URL, "SERVER_URL");
        checkNotNull(SENDER_ID, "SENDER_ID");
        // Make sure the device has the proper dependencies.
        GCMRegistrar.checkDevice(this);
        // Make sure the manifest was properly set - comment out this line
        // while developing the app, then uncomment it when it's ready.
        // NOT required any more GCMRegistrar.checkManifest(this);

        /**
         * this code to register reciver moved to message actvity
         */
        //registerReceiver(mHandleMessageReceiver, new IntentFilter(
        //      DISPLAY_MESSAGE_ACTION));


        /* final String */regId = GCMRegistrar.getRegistrationId(this);

        /**
         * save regId in pref to be used by Location update service
         */
        SavePreferences("regId", regId);

        if (regId.equals("")) {
            // Automatically registers application on startup.
            GCMRegistrar.register(this, SENDER_ID);
        } else {
            // Device is already registered on GCM, check server.
            if (GCMRegistrar.isRegisteredOnServer(this)) {
                ;;
                // Skips registration.
                // -- mDisplay.append(getString(R.string.already_registered) +
                // "\n");
            //  System.out.println(getString(R.string.already_registered)
                //      + "\n");

            } else {
                // Try to register again, but not in the UI thread.
                // It's also necessary to cancel the thread onDestroy(),
                // hence the use of AsyncTask instead of a raw thread.
                final Context context = this;
                mRegisterTask = new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        boolean registered = ServerUtilities.register(context,
                                regId);
                        // At this point all attempts to register with the app
                        // server failed, so we need to unregister the device
                        // from GCM - the app will try to register again when
                        // it is restarted. Note that GCM will send an
                        // unregistered callback upon completion, but
                        // GCMIntentService.onUnregistered() will ignore it.
                        if (!registered) {
                            GCMRegistrar.unregister(context);
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        mRegisterTask = null;
                    }

                };
                mRegisterTask.execute(null, null, null);
            }
        }
checkNotNull(服务器URL,“服务器URL”);
checkNotNull(发送者标识,“发送者标识”);
//确保设备具有正确的依赖项。
GCMRegistar.检查装置(本);
//确保清单设置正确-注释此行
//开发应用程序时,请在应用程序准备就绪时取消注释。
//不再需要任何GCMRegistar.checkManifest(此);
/**
*此用于注册接收器的代码已移动到消息ACTIVITY
*/
//registerReceiver(mHandleMessageReceiver,新意向过滤器(
//显示(信息(动作));
/*最后一个字符串*/regId=gcmregistar.getRegistrationId(this);
/**
*将regId保存在pref中以供位置更新服务使用
*/
保存首选项(“regId”,regId);
if(regId.equals(“”){
//启动时自动注册应用程序。
GCMRegistar.register(这是发送者的ID);
}否则{
//设备已在GCM上注册,请检查服务器。
if(GCMRegistrar.isRegisteredOnServer(本)){
;;
//跳过注册。
//--mDisplay.append(getString(R.string.已注册)+
//“\n”);
//System.out.println(getString(R.string.ready\u registered)
//+“\n”);
}否则{
//尝试再次注册,但不要在UI线程中注册。
//还需要取消onDestroy()线程,
//因此,使用AsyncTask而不是原始线程。
最终上下文=此;
mRegisterTask=newasynctask(){
@凌驾
受保护的Void doInBackground(Void…参数){
boolean registed=ServerUtilities.register(上下文,
雷吉德);
//此时,所有尝试向应用程序注册的用户
//服务器失败,因此我们需要注销设备
//从GCM-应用程序将在
//它已重新启动。请注意,GCM将发送
//完成时未注册的回调,但
//gcminentService.onUnregisted()将忽略它。
如果(!已注册){
GCMRegistar.unregister(上下文);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
mRegisterTask=null;
}
};
mRegisterTask.execute(null,null,null);
}
}

编辑:注意,我可以在设备上接收消息

有一段时间,单个设备只有一个注册id,不可能有多个id

当您第一次运行应用程序时,您将获得注册id,并且您必须将其注册到GCM注册器。否则消息将不会发送到您的设备

在服务器端,您需要安全地保留此注册id,以便在发送消息时可以使用它


您跟踪特定设备的注册id的问题:当您获得设备的注册id时,如果您正在使用,请将其保存在服务器数据库中,否则请保留它数组。每当生成新的注册id时,请删除以前的注册id并将新条目添加到数据库中。

谢谢。。。这里的诀窍是我需要如何为同一个设备生成一个新的ID,以便我可以删除旧的ID?sry。。生成新id后,您将尝试将其注册为GCM注册器。当此新注册id成功注册时,将调用onRegister()服务方法。在这里,您可以添加代码,用新代码替换以前的代码@重写受保护的void onRegistered(上下文,字符串注册ID){ServiceUtilities.Registers(上下文,注册ID);}sry,但我无法在注释部分正确缩进上述代码,无法将此代码作为另一个代码添加,因为它违反了stackoverflow规则。从下次开始,请以单独问题的形式提出新问题我在这里提出了一个新问题