Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM with product flavors

I have GCM sample android gradle project. Its worked well, when I add 2 flavors a push notification stopped to work. My compilation manifest (it's taken from app\build\intermediates\manifests\ex\debug) file is below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.flavor.app"
    <uses-permission android:name="com.flavor.app.permission.C2D_MESSAGE" />

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

        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

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

        <service android:name="com.ex.app.GCMIntentService" />
        <service
            android:name="com.ex.app.AppLocationService"
            class=".AppLocationService" >
            <intent-filter>
                <action
                    android:name=".AppLocationService"
                    android:value=".AppLocationService" />
            </intent-filter>
        </service>

What should I do to fix this problem? Please help.

UPD1. I am using gradle v.0.12+. i think my final manifest file looks good, GCMRegistrar.checkManifest(this); - without errors, but GCMRegistrar.isRegistered(this) always false. =(

UPD2. My first flavor project with original package name (as project in main branch) works well, but second flavor with changed package doesn't work (registrationId for push is still empty), but in manifest file all permitions are correct.

like image 327
Gorets Avatar asked Aug 18 '14 07:08

Gorets


People also ask

How do I use Android product flavors?

Creating product flavors is similar to creating build types: add them to the productFlavors block in your build configuration and include the settings you want. The product flavors support the same properties as defaultConfig —this is because defaultConfig actually belongs to the ProductFlavor class.

How do I use a different Google services JSON file with multiple product flavors Android?

How to use this file for different product flavors. There are only 3 steps first step is so simple just remove the google-service. json file from the root of the app/ module and save it your local system directory for the save side, & then make your product flavors like Production & Staging environment.

When would you use a product Flavour in your build setup?

123 let's go Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

What is a build type and a product flavor in Android?

buildType is the how of the build. flavor is the what of the build.


2 Answers

Simpy use the ${applicationId} alias like this

<permission android:name=            "${applicationId}.permission.C2D_MESSAGE"
            android:protectionLevel= "signature" />
<uses-permission android:name=       "${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name=       "com.google.android.c2dm.permission.RECEIVE" />

And for the corresponding receiver use this

    <receiver
        android:name = "com.mixpanel.android.mpmetrics.GCMReceiver"
        android:permission = "com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name = "com.google.android.c2dm.intent.RECEIVE"/>
            <action android:name = "com.google.android.c2dm.intent.REGISTRATION"/>

            <category android:name = "${applicationId}"/>
        </intent-filter>
    </receiver>
like image 65
Martin Mlostek Avatar answered Oct 14 '22 15:10

Martin Mlostek


I found solution in this post GCM not registering when changing package name with Gradle. I just override BroadcastReceiver, if anyone can explain why it helpes please tell me.

like image 1
Gorets Avatar answered Oct 14 '22 16:10

Gorets