Android GCM与产品风格

11

我有一个GCM的Android Gradle项目示例。当我添加了两种风味时,推送通知停止工作了。我的编译清单(它来自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>

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

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

更新2:我的第一个风味项目使用原始包名称(作为主分支中的项目)运行良好,但是更改了包的第二个风味项目无法正常工作(推送的registrationId仍为空),但在清单文件中所有权限都是正确的。

2个回答

38

只需像这样使用${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>

@Gorets 这个对我有效,而且我认为这是一个直接的答案... 你应该尝试这个并将其标记为答案。 - Jenny Kim
对我来说完美地运作了。谢谢 :) - Valentin
我们应该如何处理接收器? - wapples
在Flovers中,我们有相同的项目ID和服务器密钥,对于两个Flovers而言是正确的。 - andro

1

1
请检查我的回答,以获得一个更简单的解决方案。 - Martin Mlostek
上次它没有帮助,只有在覆盖BroadcastReceiver的情况下才能正常工作。 - Gorets

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接