使用Gradle更改包名称时GCM无法注册

时间:2014-06-11 23:40:09

标签: android gradle push-notification google-cloud-messaging android-gradle

在我的常规构建中,gcm工作正常,但是当我使用flavor改变com名称时,事情就停止了。我的IntentService永远不会被初始化。我应该如何设置我的清单文件以便发生这种情况?

  • 我已将它们添加到开发者控制台。
  • 我正在使用gradle com.android.tools.build:gradle:0.11。+'

我得到了日志

V/GCMBroadcastReceiver﹕ onReceive: com.google.android.c2dm.intent.REGISTRATION
V/GCMBroadcastReceiver﹕ GCM IntentService class: com.sample.app.dev.GCMIntentService
V/GCMBaseIntentService﹕ Acquiring wakelock

然后什么都没有。我的自定义IntentService类永远不会被初始化,我的registrationId保持为空。我当前的实现类似于此处的实现:https://stackoverflow.com/a/20334397/198034

我应该如何设置清单以获得使用gcm的gradle 0.11。+ flavors?

下面是我的一个版本的清单,我在主清单中有更多代码(所有需要的权限等),但没有其他处理gcm。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.app"
    android:versionCode="45"
    android:versionName="0.6.5" >

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


    <permission android:name="com.sample.app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.sample.app.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name" >


        <service android:name=".GCMIntentService" />
        <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="${packageName}" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:10)

这是我的解决方案:

<permission android:name="com.sample.app.permission.C2D_MESSAGE"
     android:protectionLevel="signature" />
 <uses-permission android:name="com.sample.app.permission.C2D_MESSAGE" />

...

<service android:name=".GCMIntentService" />
    <receiver
        android:name=".MyBroadcastReceiver"
        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.sample.app" />
        </intent-filter>
    </receiver>

在我的清单和mybroadcastReceiver

...

   public class MyBroadcastReceiver extends GCMBroadcastReceiver
   {
    @Override
    protected String getGCMIntentServiceClassName(Context context)
    {
        return GCMIntentService.class.getName();
    }
    }

答案 1 :(得分:0)

在您的清单中,您的应用包名称似乎为com.sample.app,这与您的permission.C2D_MESSAGE定义一致。您的接收者的意图过滤器类别为${packageName}。希望它也可以转换为com.sample.app

除此之外,上面发布的日志显示您的意图服务预计位于com.sample.app.dev.GCMIntentService。这是一个不同的包。

您似乎正在使用旧的已弃用的GCM客户端库,其中com.google.android.gcm.GCMBroadcastReceiver期望意图服务被调用GCMIntentService并位于应用程序的主程序包中:

/**
 * Gets the default class name of the intent service that will handle GCM
 * messages.
 */
static final String getDefaultIntentServiceClassName(Context context) {
    String className = context.getPackageName() +
            DEFAULT_INTENT_SERVICE_CLASS_NAME;
    return className;
}

您的清单声明意图服务类位于您应用的主程序包中:

<service android:name=".GCMIntentService" />

所有这些事情都没有加起来。您应用的应用包名称为com.sample.app.devcom.sample.app。如果是前者,则您发布的清单不正确。如果是后者,则广播接收器没有理由期望意图服务类为com.sample.app.dev.GCMIntentService

相关问题