注册 GCM 后 Android 5 空推送通知

标签 android push-notification

我现在看到 gcm 消息的一个非常奇怪的行为 - 在我为推送通知注册设备后,我收到一条空消息。仅一次,但针对每台运行 android 5+ 的设备。这是正常行为吗? 我的服务器开发人员发誓他不会在设备注册时向我发送任何信息...

代码: 报名:

            String regid = Preferences.getC2DMKey(ApplicationXXX.getInstance());
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(ApplicationXXX.getInstance());
            try {
                    regid = gcm.register(Customization.getGcmCode());
                    Preferences.setC2DMKey(ApplicationXXX.getInstance(), regid);
                    Log.d(TAG, "GCM key received! key:"+regid); 
            } catch (IOException ex) {
                Log.w(TAG, "Unable to update GCM key", ex); 
            }
            //say regid to server
            if (!TextUtils.isEmpty(regid)) {
                requests().reportPushNotification(regid);
            }

ApplicationXXX 是 Application 的派生类。

list :

   <permission android:name="com.xxx.permission.C2D_MESSAGE" android:protectionLevel="signature" />
   <uses-permission android:name="com.xxx.permission.C2D_MESSAGE" />
   <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
   <application...>
        <receiver
        android:name="com.pinbonus.gcm.ReceiverGCM"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.xxx" />
                <category android:name="com.xxx.gcm" />
            </intent-filter>
        </receiver>
    </application>

最佳答案

很抱歉从另一个问题复制了我的回答,但我想你可能没有看到我的评论,我想我的回答可以帮助你:

我遇到了完全相同的问题,并在这里找到了解决方案:Weird push message received on app start

每次我的应用程序重新安装时,用于推送消息的 BroadcastReceiver 都会收到一个 Intent,我会像处理普通推送通知一样处理它(这会导致向用户显示空通知)。显然,谷歌开始发送与常规推送消息具有相同 Intent 过滤器的 Intent ,以便处理推送 token 的刷新。如果您查看 google 在 Android 上实现推送客户端的文档,您会发现它现在建议我们停止创建 BroadcastReceivers,并使用 googles GCMReceiver(参见 https://developers.google.com/cloud-messaging/android/client)

由于当时我不打算重新实现我的推送客户端,所以我必须过滤 Intent 并弄清楚哪些是由 GCM 生成的,哪些是由我的推送服务器发送的。在这两种情况下,我总能在 Intent 的额外内容中得到一个名为“from”的字段,所以我用它来过滤。 Google 发起的所有 Intent 都有“google.com/iid”作为这个字段,其他通知上面有我的项目编号(例如:“from”:“42352342352423”)

String from = extras.getString("from");
if (!"google.com/iid".equals(from)) {
    // create notification       
} 

希望对你有帮助

关于注册 GCM 后 Android 5 空推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30958022/

相关文章:

Android:单击 ListView 中的空白区域会使现有项目变黑

Android 自定义字体 (nunito) 未在 TextView 中正确设置

java - 如何获取电池电量

android - 从 Rails 应用程序向 iOS、Android、BlackBerry 发送通知

android - 在android中点击通知检测应用程序是否正在运行

android - "Upstream Google Cloud Messaging and User Notifications Sign-up"是强制性的吗?

java - 将值从 Activity 传递到 Android Fragment

android - Mock Retrofit 错误 okhttp3.ResponseBody$1 无法转换

javascript - 服务 worker 的推送事件未在多个选项卡中调用

push-notification - Onesignal推送通知高级定位