android GCM receiver triggered after install and s

2019-07-10 15:03发布

问题:

I have class that inheritance NotificationHandler from azure in my project. Everything works fine before i update my project (without any changes in the receiver). Now, every time i install the application, the NotificationHandler always triggered and send empty notification. I think my problem similiar with this question .

this is the source code

<receiver  android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
           android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="_mypackage" />
            </intent-filter>
        </receiver>

Any help will be appreciated

回答1:

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