GCM具有两个不同的工作注册ID注册(GCM registering with two differ

2019-08-21 01:05发布

I'm having a slight issue with device registration management on my client/server for push notifications.

Problem

The problem that I'm having is that when I uninstall the application and re install it the application returns an empty string for the registration id (GCMRegistrar.getRegistrationId(this)) which I re-register the device with my server. The problem is that I get a new, different registration id (sometimes) and BOTH WORK! So I don't know how server side to know whether it's for the same device. I should also note I'm not changing the application version. Do any changes to the manifest trigger a new registration id to be issued?

On the Android side, I do the following:

    /**
     * Registers this android device with GCM
     */
    private void registerDeviceWithGCM() {
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            log.debug("Registering application with GCM");
            GCMRegistrar.register(this, ApplicationData.SENDER_ID);
        } else {
            log.debug("Already registered: " + regId);
            deviceRegistrationService.updateServerRegistrationData(this, regId);
        }
    }

In my GCMIntentService.java I do the following:

    /**
     * Triggered upon new device registration and updates registration info with the server
     *
     * @param context context received from
     * @param regId   device's registration id
     */
    @Override
    protected void onRegistered(Context context, String regId) {
        Log.d(TAG, "Registering: " + regId);
        Intent intent = new Intent(RegistrationReceiver.REGISTRATION_INTENT);
        intent.putExtra(RegistrationReceiver.REGISTRATION_ID, regId);
        context.sendBroadcast(intent);
    }

In my RegistrationReceiver.java I have the following:

    /**
     * Triggers the device registration with cirrus
     *
     * @param context unused
     * @param intent  used to get registration id
     */
    @Override
    public void handleReceive(Context context, Intent intent) {
        log.debug("Received registration with intent action: " + intent.getAction());
        if (intent.getAction().equals(REGISTRATION_INTENT)) {
            String regId = intent.getStringExtra(REGISTRATION_ID);
            log.debug("Received registration intent with registration id: " + regId);
            deviceRegistrationService.updateServerRegistrationData(loginActivity, regId);
        } else if (intent.getAction().equals(REGISTRATION_FAILED_INTENT)) {
            log.debug("Received registration failed intent, displaying error message");
            showRegistrationFailedMessage(intent);
        }
    }

Again, the problem here is that I have two ore more registration id's that all work (it wouldn't be a problem if I the old one simply didn't work when trying to post a message from the server as I can simply clean that up).

Answer 1:

有时,谷歌改变了注册ID,你就会有相关的多个ID。 发送通知(服务器),服务器必须更新为新ID数据库。

欲了解更多信息查看本文档:

http://developer.android.com/google/gcm/adv.html

说的是:

在服务器端,只要应用程序表现很好,一切都应该正常工作。 但是,如果在应用程序中的错误触发同一设备的多个注册,也可以是难以调和的状态,你可能会出现重复的消息。

GCM提供了一个名为“规范注册ID”容易地从这些情况恢复设施。 一个规范的注册ID被定义为您的应用程序所要求的最后注册的ID。 这是将消息发送到设备时,服务器应使用的ID。

如果稍后尝试使用不同的登记ID来发送消息,GCM将处理该请求如常,但将包括在该响应的registration_id字段规范注册ID。 请务必将存储在您的服务器与此规范的ID的注册ID,因为最终你正在使用将停止工作的ID。



文章来源: GCM registering with two different working registration ids