Android 5 empty push notification after registerin

2019-06-03 06:37发布

问题:

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>

回答1:

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