android - 获取 GCM 错误 : INVALID_SENDER occasionally in an app that has two GCM BroadcastReceivers

标签 android push-notification google-cloud-messaging

我继承了一个已经实现了 GCM 服务并且运行良好的应用程序。

我说得很好,因为有一半的时间,当应用程序启动时,我收到错误 INVALID_SENDER 而另一半我不明白!

我收到错误的次数和没有收到错误的次数没有区别(或者我可能忽略了区别)。

我确实不时收到来自 GCM 的消息(当我在登录后没有收到 INVALID_SENDER 时)

这是我主要 Activity 的onCreate()中的注册码

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

我的 list 文件

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

我的应用大约每周都有一个新版本,我读到应用更新后我需要注册一个新 ID。这可能是问题所在吗?

相关问题: Getting INVALID_SENDER on one device while its working with another GCM android

最佳答案

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

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

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。只有在这种情况下,您才应该处理广播。如果是不同的发件人 ID,则可能是图书馆使用的发件人 ID,您应该让图书馆处理它。

如果这不能解决问题,您可以考虑在 list 中只声明您的接收器,并在必要时将 GCM 广播转发给其他接收器。

关于android - 获取 GCM 错误 : INVALID_SENDER occasionally in an app that has two GCM BroadcastReceivers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734335/

相关文章:

java - Gradle中的错误

java - 如何排除/storage/emulated/0/Ringtones中的文件

android - 无需谷歌云消息(GCM)将通知从 PC 推送到 android

ios - Swift 在发布时将音频文件添加到 bundle 中

google-cloud-messaging - 一台设备到另一台设备的 Android GCM 消息

java - 如何从我的 Android Fragment 获取信息到 MyActivity.java

android - 禁用即时运行时,Gradle 构建失败并显示 "Could not read path"

ios - 如何知道推送通知发送状态

android - Parse.com 禁用推送通知通知

android - 当手机离线时,GCM 存储消息列表或仅存储最后一条消息,就像 APNS 一样?