0

在服务器端,我发现一台设备有多个注册 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);
            }
        }

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

4

1 回答 1

1

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

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

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

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

于 2013-02-22T18:04:45.763 回答