GCM有两个不同的工作登记IDS注册 [英] GCM registering with two different working registration ids

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

问题描述

我在我的客户机/服务器的推送通知上具有设备注册管理一个轻微的问题。

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

这是我遇到的问题是,当我卸载应用程序,并重新安装应用程序返回的注册ID为空字符串( GCMRegistrar.getRegistrationId(本)),我用我的服务器重新注册设备。问题是,我得到一个新的,不同的注册ID(有时)两者的工作!所以,我不知道该怎么服务器端知道它是否是同一设备。我还应该注意到,我在不改变应用程序的版本。做任何改动的清单引发新注册ID将发行?

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?

在Android的一面,我做到以下几点:

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

在我的 GCMIntentService.java 我做到以下几点:

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

在我的 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的所有工作(它不会,如果我尝试发布消息,从服务器,我可以当旧的根本没有工作的问题简单地清理一下)。

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

推荐答案

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

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:

<一个href="http://developer.android.com/google/gcm/adv.html">http://developer.android.com/google/gcm/adv.html

这是说:

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

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提供了一个名为规范注册的ID来轻易地从这些情况中恢复设施。一个规范的注册ID被定义为您的应用程序所要求的最后注册的ID。这是将消息发送到设备时,服务器应使用的ID。

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.

如果您稍后尝试使用不同的注册ID发送消息,GCM将处理该请求像往常一样,但它会在响应的registration_id领域的规范注册ID。请务必将储存在你的服务器,这个规范ID的注册ID,因为最终你使用会停止工作的ID。

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有两个不同的工作登记IDS注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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