Android Gradle Product Flavors with Parse Push Alerts

I am trying to send push notifications via Parse and integrate the tastes of the product. When I sell product flavors, I cannot receive Parse Push release alerts. Anyone have any recommendations on how to fix this problem?

Gradle application file:

apply plugin: 'com.android.application' android { compileSdkVersion 19 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.example.project" minSdkVersion 16 targetSdkVersion 21 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } productFlavors{ freeApp{ applicationId "com.example.project.free" } premiumApp{ applicationId "com.example.project.paid" } } } dependencies { compile files('libs/android-async-http-1.4.2-66-g4b6eb97.jar') compile files('libs/android-support-v13.jar') compile files('libs/bolts-android-1.2.0.jar') compile files('libs/Parse-1.9.2.jar') compile files('libs/ParseCrashReporting-1.9.2.jar') } 

Android Manifest

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.project" android:versionCode="1" android:versionName="1.1" > <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.project.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.project.permission.C2D_MESSAGE" /> <application android:name="com.example.project.Application" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name="com.example.project.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_launcher"/> <service android:name="com.parse.PushService" /> <receiver android:name="com.parse.ParseBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> <receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false"> <intent-filter> <action android:name="com.parse.push.intent.RECEIVE" /> <action android:name="com.parse.push.intent.DELETE" /> <action android:name="com.parse.push.intent.OPEN" /> </intent-filter> </receiver> <receiver android:name="com.parse.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" /> <!-- IMPORTANT: Change "com.parse.starter" to match your app package name. --> <category android:name="com.example.project" /> </intent-filter> </receiver> </application> 

+6
source share
2 answers

NOTE: As noted, my decision is somewhat more complicated than that of Morten Holmgaard below. Of course, if I knew of a simpler solution, I would suggest this. However, my answer contains some relevant explanation, and it was also the only and only correct answer for five weeks, so I will leave it.

==================================================== ===========================

What is the problem?

I think the reason you don't get any clicks is because in your AndroidManifest.xml you declare the following for Parse:

 <permission android:name="com.example.project.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.project.permission.C2D_MESSAGE" /> <receiver android:name="com.parse.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" /> <!-- IMPORTANT: Change "com.parse.starter" to match your app package name. --> <category android:name="com.example.project" /> </intent-filter> </receiver> 

Package names, however, are defined as

 applicationId "com.example.project.free" 

and

 applicationId "com.example.project.paid" 

Thus, the package names do not match the declarations in your AndroidManifest.xml, and therefore Parse cannot receive a click. In fact, if you look at your logcat output, you should see a message from Parse that tells you exactly what is missing in your AndroidManifest.xml.

So how to solve this?

This is a bit complicated situation, but it can be done:

1.) Remove the two parts that I quoted above from AndroidManifest.xml in the original main set ( src/main/AndroidManifest.xml ).

2a.) Create AndroidManifest.xml in the original free set ( src/free/AndroidManifest.xml ) and enter the following:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <permission android:name="com.example.project.free.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.project.free.permission.C2D_MESSAGE" /> <application> <receiver android:name="com.parse.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.example.project.free" /> </intent-filter> </receiver> </application> </manifest> 

2b.) Do the same for the paid initial set. Be sure to replace the package name correctly in AndroidManifest.xml.

Why does it work?

Since gradle does not replace src/main/AndroidManifest.xml with src/free/AndroidManifest.xml , but instead merges them into one. Therefore, if you simply leave the ads from the main source set and place them in free and paid , you will get correctly combined AndroidManifest.xml for each flavor.

+2
source

I just very easily solved the same problem with Manifest Merger :

  ${applicationId} 

Example:

  <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" /> <application> <receiver android:name="com.parse.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="${applicationId}" /> </intent-filter> </receiver> </application> 
+8
source

Source: https://habr.com/ru/post/989771/


All Articles