在具有两个 GCM 广播接收器的应用程序中偶尔出现 GCM 错误:INVALID_SENDER [英] Getting GCM Error: INVALID_SENDER occasionally in an app that has two GCM BroadcastReceivers

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

问题描述

我继承了一个已经实现 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)

这是我主activity的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;
                }

我的清单文件

 <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?

相关问题:同时在一台设备上获取 INVALID_SENDER它与另一个 GCM android 一起工作

推荐答案

GCM 发送的广播似乎是有序的.这意味着它们一次交付一个.有时,库的接收器可能会在您的接收器之前被调用,这可能会阻止您的接收器被调用(如果它取消广播).

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

有几种处理方法.一种方法是为您的接收器提供比图书馆接收器更高的优先级.只需将 android:priority 属性添加到两个接收器的 intent-filter 中,并为您的接收器提供更高的优先级.

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 发送)一次传送给一个接收器.当每个接收器依次执行时,它可以将结果传播到下一个接收器,或者它可以完全中止广播以便它不会传递给其他接收器.可以使用匹配的意图过滤器的 android:priority 属性控制运行的顺序接收器;具有相同优先级的接收器将按任意顺序运行.

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 ().get("from") 是否包含您正在使用的发送者 ID.只有在这种情况下,您才应该处理广播.如果是不同的sender ID,很可能是图书馆使用的sender 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 广播接收器的应用程序中偶尔出现 GCM 错误:INVALID_SENDER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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