获取GCM错误:INVALID_SENDER偶尔在一个应用程序,有两个GCM BroadcastReceivers [英] Getting GCM Error: INVALID_SENDER occasionally in an app that has two GCM BroadcastReceivers

查看:201
本文介绍了获取GCM错误:INVALID_SENDER偶尔在一个应用程序,有两个GCM BroadcastReceivers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了已经实施GCM服务,并正在应用不够好。

I inherited an application that already implemented GCM services and is working well enough.

我说的不够好,因为一半的时候,当应用程序启动后,我得到的错误 INVALID_SENDER ,另一半我不明白!

I'm saying well enough because half the times, when the app launches, I get the error INVALID_SENDER and the other half I don't get it!

还有我的错误的时候,我不(或也许我缺少的差异)的时间之间没有差别。

There is no difference between the times I get the error and the times I don't (or maybe I'm missing the difference).

我得到的消息从GCM不时(当时我没有得到 INVALID_SENDER 登录后)

I do get messages from GCM from time to time (when I don't get the INVALID_SENDER after logging in)

这是注册code。在的onCreate()我的主要活动

This is the registration code in the onCreate() of my main activity

private void registerGCM() throws Exception {
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            CommonUtilities.DISPLAY_MESSAGE_ACTION));
    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {

        // Automatically registers application on startup.
        GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
    } else {


        // Device is already registered on GCM, check server.
        if (GCMRegistrar.isRegisteredOnServer(this)) {


            ServerUtilities.register(mContext, regId);
        } 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);
                    if (!registered) {
                         GCMRegistrar.unregister(context);
                    }
                    return null;
                }

我的清单文件

My Manifest file

 <receiver
        android:name="com.mixpanel.android.mpmetrics.GCMReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.says.broadcaster" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.says.broadcaster" />
        </intent-filter>
    </receiver>

我之所以有两个接收器是我从统计数据跟踪API,我使用过推送通知,和他们使用的GCM。

The reason I have two receivers is that I get push notifications from the stats tracking API I'm using too, and they use GCM.

我的应用程序每星期左右,有一个新的版本,我读,我需要注册一个应用程序更新后,一个新的ID。难道这是问题?

My app has a new version every week or so, and I was reading that I need to register for a new ID after an app update. Could this be the issue?

相关问题: <一href="http://stackoverflow.com/questions/15630331/getting-invalid-sender-on-one-device-while-its-working-with-another-gcm-android">Getting INVALID_SENDER一台​​设备上,而它的另一个GCM安卓工作

推荐答案

通过GCM发送的广播似乎排序。这意味着他们同时交付使用。这有可能是图书馆的接收器有时你的接收器之前被调用,它可能prevent您的接收器被调用(如取消广播)。

The Broadcasts sent by GCM seem to be ordered. This means they are delivered one at a time. It's possible that the receiver of the library sometimes gets called before your receiver, and it might prevent your receiver from being called (if it cancels the broadcast).

有几种方法来处理它。一种方法是给你的接收器的优先级高于图书馆的接收器。只需添加安卓优先属性不仅可以接收的意图过滤器,并给您的接收器更高的优先级。

There are several ways to handle it. One way is to give your receiver a higher priority than the library's receiver. Just add the android:priority attribute to the intent-filter of both receiver, and give your receiver a higher priority.

序广播(与Context.sendOrderedBroadcast发送)被输送到一个接收机的时间。由于每个接收器依次执行时,它可以传播的结果到下一个接收器,或者它可以完全中止该广播,以便它不会被传递给其他接收机。订单接收运行可以与机器人控制:匹配意图过滤器的优先级属性;接收机具有相同优先级可以为任意的顺序来运行。

Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.

当您的广播接收器会被调用,你应该检查 intent.getExtras()获得(自)包含您正在使用的发件人ID。只有在这种情况下,你应该处理的广播。如果它是一个不同的发送者ID,它可能是库使用的发送者ID,并且你应该让库处理。

When your broadcast receiver gets called, you should check that intent.getExtras ().get("from") contains the sender ID you are using. Only in this case you should handle the broadcast. If it's a different sender ID, it's probably the sender ID used by the library, and you should let the library handle it.

如果不解决这个问题,可以考虑宣布只有你的接收器清单,并转发GCM广播到其它接收器在必要时。

If that doesn't solve the problem, you can consider declaring only your receiver in the manifest, and forwarding the GCM broadcast to the other receiver when necessary.

这篇关于获取GCM错误:INVALID_SENDER偶尔在一个应用程序,有两个GCM BroadcastReceivers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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