GCM 使用两个不同的工作注册 ID 进行注册 [英] GCM registering with two different working registration ids

查看:17
本文介绍了GCM 使用两个不同的工作注册 ID 进行注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端/服务器上的设备注册管理有一个小问题,用于推送通知.

问题

我遇到的问题是,当我卸载应用程序并重新安装它时,应用程序返回一个空字符串作为注册 ID (GCMRegistrar.getRegistrationId(this)),我重新 -在我的服务器上注册设备.问题是我得到了一个新的、不同的注册 ID(有时)并且两者都有效!所以我不知道服务器端如何知道它是否用于同一设备.我还应该注意我没有更改应用程序版本.清单的任何更改是否会触发新的注册 ID 的发布?

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

<前>/*** 使用 GCM 注册此 android 设备*/私有无效 registerDeviceWithGCM() {GCMRegistrar.checkDevice(this);GCMRegistrar.checkManifest(this);String regId = GCMRegistrar.getRegistrationId(this);如果(regId.equals(")){log.debug("使用 GCM 注册应用程序");GCMRegistrar.register(this, ApplicationData.SENDER_ID);} 别的 {log.debug("已经注册:" + regId);deviceRegistrationService.updateServerRegistrationData(this, regId);}}

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

<前>/*** 在新设备注册时触发并更新与服务器的注册信息** @param 上下文上下文从* @param regId 设备的注册id*/@覆盖protected void onRegistered(Context context, String regId) {Log.d(TAG, "注册:" + regId);意图意图=新意图(RegistrationReceiver.REGISTRATION_INTENT);intent.putExtra(RegistrationReceiver.REGISTRATION_ID, regId);context.sendBroadcast(意图);}

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

<前>/*** 使用 cirrus 触发设备注册** @param 上下文未使用* @param intent 用于获取注册id*/@覆盖公共无效句柄接收(上下文上下文,意图意图){log.debug("收到注册意向操作:" + intent.getAction());如果 (intent.getAction().equals(REGISTRATION_INTENT)) {String regId = intent.getStringExtra(REGISTRATION_ID);log.debug("收到注册意向,注册id:" + regId);deviceRegistrationService.updateServerRegistrationData(loginActivity, regId);} else if (intent.getAction().equals(REGISTRATION_FAILED_INTENT)) {log.debug("收到注册失败意图,显示错误信息");showRegistrationFailedMessage(意图);}}

同样,这里的问题是我有两个或更多的注册 ID,它们都可以工作(如果我在尝试从服务器发布消息时,旧的根本不起作用,那也不是问题)只需清理它).

解决方案

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

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

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

也就是说:

<块引用>

在服务器端,只要应用程序运行良好,一切都应该正常运行.但是,如果应用程序中的错误触发了同一设备的多次注册,则可能很难协调状态,最终可能会收到重复的消息.

GCM 提供了一种称为规范注册 ID"的工具.轻松地从这些情况中恢复过来.规范的注册 ID 被定义为您的应用程序请求的最后一个注册的 ID.这是服务器在向设备发送消息时应使用的 ID.

如果稍后您尝试使用不同的注册 ID 发送消息,GCM 将照常处理请求,但它会在响应的 registration_id 字段中包含规范的注册 ID.请务必使用此规范 ID 替换存储在服务器中的注册 ID,因为最终您使用的 ID 将停止工作.

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).

解决方案

Sometimes Google changes the registration ID and you'll have multiple IDs associated. The server that sends the notification (your server) has to update the database with the new ID.

For more info check this document:

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

That says:

On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.

If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.

这篇关于GCM 使用两个不同的工作注册 ID 进行注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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