GcmBroadcastReceiver not fired on Android 4.0.3

2019-06-24 14:14发布

问题:

I've implemented GCM in my app, following this official tutorial. But my users under Android 4.0.3 reported me notifications are not working. I found out that onReceive from my GcmBroadcastReceiver extends BroadcastReceiver wasn't fired. Here is my Manifest.

    <!-- GCM -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.VIBRATE" />

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

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

    <application
        ... >

        <!-- GCM -->
        <receiver
            android:name="com.myapp.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.myapp" />
            </intent-filter>
        </receiver>

What am I doing wrong?

回答1:

Is your application's main package name com.nyapp.gcm or com.myapp?

In the permission part of the manifest you use com.myapp.gcm while in the category of the intent filter of the receiver you use com.myapp.

In both places you should you the same package, which is the main package of your app.



回答2:

You are missing the action "com.google.android.c2dm.intent.REGISTRATION" in your filter, without which your app will not be able to receive a registration Id. Add the following to your intent-filter:

action android:name="com.google.android.c2dm.intent.REGISTRATION"



回答3:

<!-- GCM -->
    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

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

Your Manifest file permissions missing check BroadcastReceiver registration in manifest file