在GCM服务器端多应用程序ID [英] Multiple Application ID in Server side in GCM

查看:98
本文介绍了在GCM服务器端多应用程序ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器端,我找到了一个设备有多个注册ID,这显然是创造了我很多问题。像消息中收到了很多次。

如何能获得红色旧的注册的ID,或确保注册不会发生,如果有一个有效的注册ID。

我遵循时,我写我的应用程序如下Android的文档的实例教程:

  checkNotNull(SERVER_URLSERVER_URL);
            checkNotNull(SENDER_IDSENDER_ID);
            //确保设备具有适当的依赖关系。
            GCMRegistrar.checkDevice(本);
            //确保清单中正确设置 - 注释掉这行
            //在开发应用程序,则取消其注释当它准备。
            //不再要求GCMRegistrar.checkManifest(本);            / **
             *此code注册reciver搬到消息actvity
             * /
            // registerReceiver(mHandleMessageReceiver,新的IntentFilter(
            // DISPLAY_MESSAGE_ACTION));
            / *最后弦乐* / REGID = GCMRegistrar.getRegistrationId(本);            / **
             *节省REGID在$ P $粉煤通过位置更新服务使用
             * /
            节省preferences(REGIDREGID);            如果(regId.equals()){
                //自动注册上启动应用程序。
                GCMRegistrar.register(这一点,SENDER_ID);
            }其他{
                //设备已登记在GCM,检查服务器。
                如果(GCMRegistrar.isRegisteredOnServer(本)){
                    ;;
                    //跳过注册。
                    // - mDisplay.append(的getString(R.string.already_registered)+
                    //\\ n);
                //的System.out.println(的getString(R.string.already_registered)
                    // +\\ n);                }其他{
                    //尝试再次注册,但不是在UI线程。
                    //这也是必要取消线程的onDestroy()
                    //因此,使用AsyncTask的,而不是一个原始的线程。
                    最后上下文的背景下=这;
                    mRegisterTask =新的AsyncTask<太虚,太虚,太虚>(){                        @覆盖
                        保护无效doInBackground(虚空...... PARAMS){
                            布尔注册= ServerUtilities.register(背景下,
                                    REGID);
                            //此时所有尝试与应用程序注册
                            //服务器失败了,所以我们需要取消注册设备
                            //从GCM - 应用程序将尝试在重新注册
                            //重新启动它。需要注意的是GCM将发送
                            完成后//注册的回调,但
                            // GCMIntentService.onUnregistered()会忽略它。
                            如果(!注册){
                                GCMRegistrar.unregister(上下文);
                            }
                            返回null;
                        }                        @覆盖
                        保护无效onPostExecute(虚空结果){
                            mRegisterTask = NULL;
                        }                    };
                    mRegisterTask.execute(NULL,NULL,NULL);
                }
            }


解决方案

 请参考链接: http://stackoverflow.com/questions/15030233/gcm-multiple-application-id-in-server-side/15030465#comment21123039_15030465同时检查规范注册ID概念:HTTP://developer.android.com/google/gcm/adv.html要么当生成一个新的ID,你会尝试注册它GCM注册商。当这个新注册ID成功注册onRegister()服务的方法将被调用。在这里你可以添加code。与新one.but这与注册ID一起更换previous一个你需要发送一些独特的参考服务器,所以下次你可以检查这个独特的参考,并取代现有的登记编号与新检查以下code:@覆盖保护无效onRegistered(上下文的背景下,字符串registrationId){ServiceUtilities.register(背景下,registrationId);
//调用服务并更换ID
}

On server side, i found for one device there multiple registration IDs, which obviously is creating for me lots of issues. like messages received many times.

How could I get red of old registration IDs, or make sure that registration doesn't happen if there a valid registration ID.

I follow the example tutorial on Android doc when I wrote my app as below:

 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);
                }
            }

解决方案

Please refer link: http://stackoverflow.com/questions/15030233/gcm-multiple-application-id-in-server-side/15030465#comment21123039_15030465

Also check canonical registration ids concept :http://developer.android.com/google/gcm/adv.html

or 

when a new id is generated you will try to register it GCM registrar. when this new registration id successfully register your onRegister() method of service will be called . 

here you can add code to replace previous one with new one.but for this along with registration id you need to send some unique reference to server,so for next time you can check this unique reference and replace existing registration id with new one 

checked following code:

@Override 

protected void onRegistered(Context context, String registrationId) { 

ServiceUtilities.register(context, registrationId);
//call service and replace id 
}

这篇关于在GCM服务器端多应用程序ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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