首页 文章

GCM注册两个不同的工作注册ID

提问于
浏览
8

我的客户端/服务器上的设备注册管理存在轻微的推送通知问题 .

问题

我遇到的问题是当我卸载应用程序并重新安装它时,应用程序返回一个空字符串作为注册ID( GCMRegistrar.getRegistrationId(this) ),我用我的服务器重新注册设备 . 问题是我得到一个新的,不同的注册ID(有时)和两个工作!所以我不会't know how server side to know whether it'为同一台设备 . 我还应该注意,我没有更改应用程序版本 . 清单的任何更改是否会触发要发布的新注册ID?

在Android方面,我执行以下操作:

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

在我的 GCMIntentService.java 中,我执行以下操作:

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

在我的 RegistrationReceiver.java 中,我有以下内容:

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

再一次,这里的问题是我有两个以上的注册ID都可以工作(如果我在尝试从服务器发布消息时旧的那个根本不工作就不会有问题,因为我可以简单地清理它向上) .

1 回答

  • 11

    有时谷歌会更改注册ID,您将拥有多个ID . 发送通知的服务器(您的服务器)必须使用新ID更新数据库 .

    有关更多信息,请查看此文档:

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

    说的是:

    在服务器端,只要应用程序运行良好,一切都应该正常工作 . 但是,如果应用程序中的错误触发同一设备的多个注册,则可能很难协调状态,并且最终可能会出现重复消息 . GCM提供了一个名为“规范注册ID”的工具,可以轻松地从这些情况中恢复 . 规范注册ID定义为应用程序请求的最后一次注册的ID . 这是服务器在向设备发送消息时应使用的ID . 如果稍后您尝试使用不同的注册ID发送消息,GCM将照常处理请求,但它将在响应的registration_id字段中包含规范注册ID . 确保使用此规范ID替换存储在服务器中的注册ID,因为您使用的ID最终将停止工作 .

相关问题