Search code examples
androidandroid-intentpushgoogle-cloud-messagingandroid-c2dm

IntentReceiver not called with Android Versions < 4.1


I am using Android Studio. I implemented an push service in my app. It is working with all kind of devices with newer Android versions, however it won't work with an Galaxy S2 Mini with Android 2.3.6. The device receives pushs from other apps with Urban Airship libraries.

Relevant parts of the Manifest:

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

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

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

    <receiver
        android:name=".IntentReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
        >
        <intent-filter
            android:priority="100">
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="com.google.android.c2dm.intent.C2D_MESSAGE" />
            <category android:name="com.my_app.app" />

        </intent-filter>
    </receiver>

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <service android:name=".GCMIntentService"
        android:enabled="true"/>

IntentReceiver.java:

import android.support.v4.content.WakefulBroadcastReceiver;

public class IntentReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        //doing some stuff & starting my service here, 
        //never gets called with said device

    }
}

The device gets registered with push. I have to call it twice before the id is returned, but the right Id is saved in my backend (I send the push manually anyways, so that shouldn't be the issue). I also register a second receiver from code when the app is in foreground, but none of them gets called.
If I add <uses-permission android:name="android.permission.C2D_MESSAGE" /> in my Manifest, I receive an com.google.android.c2dm.intent.REGISTRATION action when I send a push.
I had the same issue with an Galaxy Tab. Before updating it received a "com.google.android.c2dm.intent.REGISTRATION" with every push (with intent.getStringExtra("unregistered") set). So instead of getting RECEIVE it just was unregistered, just like the Galaxy Mini. I then updated it to 4.2.2. It now gets a RECEIVE every push like it should. I rarely read about this very problem (not receiving pushs with < 4.1). But I didn't find a solution. I wonder if anyone experienced something similar and found a solution or if somebody sees a problem with my manifest.


Solution

  • So, apparently not all package names in the manifest are replaced by build.gradle. In my case (with Android Studio 0.8) it was the custom permission that was wrong. I am using flavours, I guess I should have mentioned that. The package name in the custom permission has to be the package name including flavour, like this:

     <permission
              android:name="com.my_app.app.myflavour.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    
    <uses-permission android:name="com.my_app.app.myflavour.permission.C2D_MESSAGE" />