Not receiving push notifications from GCM (Android

2019-05-15 22:51发布

问题:

I'm using this link to test my push notifications. Last night it worked fine, woke up this morning and I'm no longer receiving push notifications. I tested it before I wrote any new code. The device used for testing is a Samsung Galaxy S5, OS is Lollipop.

Here is my manifest, let me know if you need to see any of my services.

<permission
        android:name=".permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name=".permission.C2D_MESSAGE" />

    <application
        ....
        ....

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            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.fullpackage.name.gcm" />
            </intent-filter>
        </receiver>

        <service
            android:name=".services.MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".services.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

I get no errors and it says the message is sent successfully from both my server and that testing link above. I've tried uninstalling the app and generating a new token, and its still not working. I am also able to retrieve my token successfully each time I load the app.

回答1:

Look at this official tutorial. Seems you missed to add you package name to permissions.

<permission
    android:name="your.package.name.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="your.package.name.permission.C2D_MESSAGE" />

Also sources of yours MyGcmListenerService and MyInstanceIDListenerService would be useful to help (how you obtain and handle token etc).

Or, if you say that its worked fine yesterday, try to enable and then disable airplane mode, or reboot the phone.