对于GCM注册后,Android的5空推送通知通知、GCM、Android

由网友(忆雨)分享简介:我现在看到GCM消息很奇怪behviour - 我推送通知注册设备后,我收到一个空的消息。只是一次,而是针对每个设备运行Android 5+。这是一种正常的行为?我的服务器开发者发誓他不会对设备注册送什么给我... code:注册: 字符串REGID = preferences.getC2DMKey(Applica...

我现在看到GCM消息很奇怪behviour - 我推送通知注册设备后,我收到一个空的消息。只是一次,而是针对每个设备运行Android 5+。这是一种正常的行为?我的服务器开发者发誓他不会对设备注册送什么给我...

code:注册:

 字符串REGID = preferences.getC2DMKey(ApplicationXXX.getInstance());            Google云端通讯GCM = GoogleCloudMessaging.getInstance(ApplicationXXX.getInstance());            尝试{                    REGID = gcm.register(Customization.getGcm code());                    preferences.setC2DMKey(ApplicationXXX.getInstance(),REGID);                    Log.d(TAG获得GCM键键:+ REGID);            }赶上(IOException异常前){                Log.w(TAG,无法更新GCM钥匙,前);            }            //说REGID服务器            如果(!TextUtils.isEmpty(REGID)){                请求()reportPushNotification(REGID)。            } 

ApplicationXXX是应用程序的派生类。

清单:

 <允许机器人:名字=com.xxx.permission.C2D_MESSAGE安卓的ProtectionLevel =签名/>   <使用许可权的android:NAME =com.xxx.permission.C2D_MESSAGE/>   <使用许可权的android:NAME =com.google.android.c2dm.permission.RECEIVE/>   <应用...>        <接收        机器人:名字=com.pinbonus.gcm.ReceiverGCM        机器人:出口=真        机器人:权限=com.google.android.c2dm.permission.SEND>            &所述;意图滤光器>                <作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/>                <类机器人:名字=com.xxx/>                <类机器人:名字=com.xxx.gcm/>            &所述; /意图滤光器>        < /接收器>    < /用途> 

解决方案

对不起,从另一个问题重复我的答案,但我想也许你没有看到我的评论,我想我的回答可以帮助你:

我有完全相同的问题,在这里找到了解决办法:奇怪的推在应用程序接收到的消息开始

每次我的应用程序重新安装了推送mesages广播接收器接收到的意图,我正在处理它像一个正常的推送通知(这导致空通知用户的显示)。显然,谷歌开始发送此意图具有相同的意图过滤器作为常规推送消息,以处理推令牌刷新。如果你看一下谷歌的文档,实现在Android上推客户,你会看到,现在给大家推荐停止创造出BroadcastReceivers,以及使用谷歌GCMReceiver(见的 https://developers.google.com/cloud-messaging/android/client )

由于我不打算在那个时候重新实现我的推客户端,我不得不过滤意图,并找出了GCM,哪些由我推送服务器被送往生成哪些。在这两种情况下我总是得到一个名为从的意图的额外场内,所以我用它来过滤。由谷歌推出的所有意图已经google.com/iid作为这个领域,另一个通知了它(例如:自:42352342352423)我的项目数

 从= extras.getString字符串(从);如果(!google.com/iid.equals(从)){    //创建通知} 
Android 服务器推送技术

我希望可以帮助你。

I see a very strange behviour for gcm messages now - after I register device for push notifications, I receive an empty message. Just once, but for each device running android 5+. Is this a normal behaviour? My server developer swears that he doesn't sending anything to me on device registration...

Code: registration:

            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 is Application-derived class.

Manifest:

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

解决方案

Sorry for duplicating my answer from another question, but I think maybe you did not see my comment, and I think my answer can help you:

I had the exact same problem, and found the solution here: Weird push message received on app start

Everytime my application was reinstalled my BroadcastReceiver for push mesages received an Intent, and I was handling it like a normal push notification (which led to the display of an empty notification to the user). Apparently google started sending this intents that have the same intent filter as the regular push messages in order to handle refresh of push tokens. If you look at google's documentation for implementing push clients on Android, you will see that it now recommends us to stop creating out BroadcastReceivers, and use googles GCMReceiver (see https://developers.google.com/cloud-messaging/android/client)

Since I was not going to reimplement my push client at that time, I had to filter the intents and figure out which ones were generated by the GCM and which ones were sent by my push server. On both cases I could always get a field called "from" inside the intent's extras, so I used it to filter. All intents launched by Google had "google.com/iid" as this field, and the other notifications had my project number on it (example: "from": "42352342352423")

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

I hope that helps you

阅读全文

相关推荐

最新文章