Android Gradle product Flavors with Parse Push Notifications

前端 未结 2 1635
醉梦人生
醉梦人生 2020-12-18 04:49

I am trying to send Push notifications via Parse and integrating product flavors. When I implement product flavors, i am not able to receive Parse Push Notifications. Does a

相关标签:
2条回答
  • 2020-12-18 05:10

    I have just solved the exact same problem very easily with the 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>
    
    0 讨论(0)
  • 2020-12-18 05:22

    NOTE: As has been observed, my solution is somewhat more complicated than the one by Morten Holmgaard below. Needless to say, had I known of the simpler solution I would have proposed that one. However, my answer does contain some relevant explanation and it also was the only - and only correct - answer for five weeks, so I will leave it up.

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

    What's the problem?

    I think the reason you are not receiving any pushes is that 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's package name.
            -->
            <category android:name="com.example.project" />
        </intent-filter>
    </receiver>
    

    The package names, however, are defined as

    applicationId "com.example.project.free"
    

    and

    applicationId "com.example.project.paid"
    

    So the package names don't match the declarations in your AndroidManifest.xml and therefore Parse is unable to receive pushes. Actually, if you look at your logcat output, you should be seeing a message from Parse that tells you exactly what is missing in your AndroidManifest.xml.

    So, how to solve this?

    This is a bit of a tricky situation but it can be done:

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

    2a.) Create an AndroidManifest.xml in your free source 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 source set. Be sure to replace the package name correctly in the AndroidManifest.xml.

    Why does this work?

    Because gradle does not replace the src/main/AndroidManifest.xml with the src/free/AndroidManifest.xml but instead merges them into one. So if you just leave the declarations out of the main source set and put them in to free and paid you will get a correctly merged AndroidManifest.xml for each flavor.

    0 讨论(0)
提交回复
热议问题