GcmBroadcastReceiver not fired on Android 4.0.3

霸气de小男生 提交于 2019-12-23 13:19:05

问题


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



来源:https://stackoverflow.com/questions/17824058/gcmbroadcastreceiver-not-fired-on-android-4-0-3

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