java - 具有产品 flavor 的 Android GCM

标签 java android gradle google-cloud-messaging android-productflavors

我有 GCM 示例 android gradle 项目。它运行良好,当我添加 2 种口味时,推送通知停止工作。我的编译 list (取自 app\build\intermediates\manifests\ex\debug)文件如下:

<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>

我应该怎么做才能解决这个问题?请帮忙。

UPD1. 我正在使用 gradle v.0.12+。我认为我的最终 list 文件看起来不错,GCMRegistrar.checkManifest(this); - 没有错误,但 GCMRegistrar.isRegistered(this) 总是错误的。 =(

UPD2. 我的第一个具有原始包名称的 flavor 项目(作为主分支中的项目)运行良好,但具有更改包的第二个 flavor 不起作用(推送的 registrationId 仍然为空),但是在 list 文件中,所有许可都是正确的。

最佳答案

Simpy 像这样使用 ${applicationId} 别名

<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" />

对于相应的接收器使用这个

    <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>

关于java - 具有产品 flavor 的 Android GCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357933/

相关文章:

java - 二进制字符串正则表达式控制位置

android - 从url下载pdf,在flutter中保存到android中的手机本地存储

android - 无法解决结果的 Activity

java - 如何使用 Java 在 Linux 中获取总磁盘空间?

java - 在 select 语句中带有参数化 where 子句的 PreparedStatement 抛出异常

java - 安卓接听来电

android - 如何将本地库 (aar) 作为依赖项的一部分上传到 Bintray

java - android Gradle同步错误(匹配版本)

spring - 如何在gradle中获取依赖项的元数据?

java - opencyc 2.0 启动服务器时出错