android GCM receiver triggered after install and sending empty notif

ε祈祈猫儿з 提交于 2019-12-24 03:52:48

问题


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



来源:https://stackoverflow.com/questions/31419634/android-gcm-receiver-triggered-after-install-and-sending-empty-notif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!