GCM定制的BroadcastReceiver [英] GCM with custom broadcastreceiver

查看:127
本文介绍了GCM定制的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行GCM通知我的应用程序。因为我用我的code,产生大量的应用程序不同的包名的我无法使用标准mypackage.GCMIntentService名。当生成应用程序我做的改动只是在我的R舱的清单和变更进口。所以我impelented我自己的BroadcastReceiver

I am implementing gcm notifications in my application. Because I use my code to generate lot of application with different package names I cannot use standard mypackage.GCMIntentService name. When generating applications I do changes only in Manifest and change imports of my R class. So I impelented my own BroadcastReceiver

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

返回包名的名字GCMIntentService不管。

to return name of GCMIntentService regardless of package name.

下面是我的清单:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission
        android:name="org.rferl.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="org.rferl.permission.C2D_MESSAGE" />


    <service
        android:name="org.rferl.service.GCMIntentService"
        android:enabled="true" />


   <receiver
        android:name="org.rferl.GCMReceiver"
        android:enabled="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="org.rferl" />
        </intent-filter>
    </receiver>

一切工作正常,我可以注册,注销,接收消息。但是,当应用程序没有运行过程中出现任何GCMIntentService.onMessage不叫。我缺少的东西在我的表现?为什么系统没有启动的服务?

Everything works fine, I can register, unregister, receive messages. But when application is not runnig no GCMIntentService.onMessage is not called. Am I missing something in my manifest? Why system did not start service?

推荐答案

注意建议您验证是否可以做以下修改之前,通过GCM接收消息。要使用该应用程序的默认的包名在Android应用实现GCM,请参见 GCM:入门< /一>

To change package/class name for application/GCMIntentService/GCMBroadcastReceiver for Android GCM (using Eclipse ADT)

Note: You are advised to verify that you can receive messages through GCM before making the following changes. To implement GCM in Android app using the application's default package name, see GCM: Getting Started.

要改变您的应用程序包的名称(例如,新的包名= com.example.newpackage ),

To change package name of your app (e.g., new package name = com.example.newpackage),

  • 在Package Explorer中,右键单击该项目→Android的工具→重命名应用程序包。
    这将自动方便更新包的名称。
  • 的Andr​​oidManifest.xml ,更新包名许可使用-许可 C2D_MESSAGE

  • In Package Explorer, right click the project → Android Tools → Rename Application Package.
    This updates the package name automatically and conveniently.
  • In AndroidManifest.xml, update the package name in permission and uses-permission for C2D_MESSAGE:

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

  • 的Andr​​oidManifest.xml 更新接收:

  • In AndroidManifest.xml, update the package name in category of the receiver:

    <receiver
        android:name="com.example.oldpackage.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >  <!-- Not this one!! -->
        <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.newpackage" />  <!-- This one!! -->
        </intent-filter>
    </receiver>
    

  • 如果您的应用程序包的名称是 com.example.newpackage ,您的 GCMIntentService 必须名为 com.example.newpackage.GCMIntentService 。如果不是,

    If your app's package name is com.example.newpackage, your GCMIntentService must be called com.example.newpackage.GCMIntentService. If not,

    • 创建一个新类扩展 GCMBroadcastReceiver 并覆盖 getGCMIntentServiceClassName()

    public class MyBroadcastReceiver extends GCMBroadcastReceiver
    {
        @Override
        protected String getGCMIntentServiceClassName(Context context)
        {
            return MyIntentService.class.getName(); // Don't hard-code like "com.example.oldpackage.MyIntentService", see http://stackoverflow.com/a/936696/1402846
        }
    }
    

    这是基于<一href="http://developer.android.com/reference/com/google/android/gcm/GCMBroadcastReceiver.html">Google's在GCMBroadcastReceiver 文档:

    this is based on Google's documentation on GCMBroadcastReceiver:

    默认情况下,<一个href="http://developer.android.com/reference/com/google/android/gcm/GCMBaseIntentService.html"><$c$c>GCMBaseIntentService类属应用程序主包中,名为<一个href="http://developer.android.com/reference/com/google/android/gcm/GCMConstants.html#DEFAULT_INTENT_SERVICE_CLASS_NAME"><$c$c>DEFAULT_INTENT_SERVICE_CLASS_NAME.要使用新的类,在<一个href="http://developer.android.com/reference/com/google/android/gcm/GCMBroadcastReceiver.html#getGCMIntentServiceClassName%28android.content.Context%29"><$c$c>getGCMIntentServiceClassName(Context)必须重写。

    By default, the GCMBaseIntentService class belongs to the application main package and is named DEFAULT_INTENT_SERVICE_CLASS_NAME. To use a new class, the getGCMIntentServiceClassName(Context) must be overridden.

    的Andr​​oidManifest.xml ,更新的名称接收

    <receiver
        android:name="com.example.oldpackage.MyBroadcastReceiver"
        ... >
    </receiver>
    

  • 的Andr​​oidManifest.xml 更新服务的名称

    <service android:name="com.example.oldpackage.MyIntentService" />
    

  • 如果你改变的包/类名 GCMBroadcastReceiver

    If your changed the package/class name of GCMBroadcastReceiver:

    • 的Andr​​oidManifest.xml ,更新的名称接收

    <receiver
        android:name="com.example.oldpackage.NewBroadcastReceiver"
        ... >
    </receiver>
    

    • 验证在的Andr​​oidManifest.xml ,包的名称应该出现至少4次:

    • Verify that in AndroidManifest.xml, the package name should appear at least 4 times:

    • 清单

    <manifest ...
        package="com.example.newpackage" ...
    

  • 许可

    <permission android:name="com.example.newpackage.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    

  • 使用-许可

    <uses-permission android:name="com.example.newpackage.permission.C2D_MESSAGE" />
    

  • 意图过滤器接收的GCM广播接收器:

  • In category within intent-filter within receiver for the GCM broadcast receiver:

    <category android:name="com.example.newpackage" />
    

  • 如果你改变了你的包名,卸载旧的应用程序在测试之前,你的设备/仿真器。

    If you changed your package name, uninstall the old app on your device/emulator before testing.

    如果您在收到您的注册ID <一href="http://developer.android.com/reference/com/google/android/gcm/GCMBaseIntentService.html#onRegistered%28android.content.Context,%20java.lang.String%29"><$c$c>onRegistered(),你应该看到像这样在LogCat中(标记 GCMBroadcastReceiver ):

    If you received your registration ID in onRegistered(), you should see something like this in LogCat (tag GCMBroadcastReceiver):

    GCMBroadcastReceiver        onReceive: com.google.android.c2dm.intent.REGISTRATION
    GCMBroadcastReceiver        GCM IntentService class: com.example.oldpackage.MyIntentService
    

    验证意图服务的包/类的名称是正确的。

    Verify that the package/class name of the intent service is correct.

    如果您覆盖<一个href="http://developer.android.com/reference/com/google/android/gcm/GCMBroadcastReceiver.html#getGCMIntentServiceClassName%28android.content.Context%29"><$c$c>getGCMIntentServiceClassName(Context)在自己的 GCMBroadcastReceiver ,你应该看到这样的事情在LogCat中(标记 GCMRegistrar ):

    If you override getGCMIntentServiceClassName(Context) in your own GCMBroadcastReceiver, you should see something like this in LogCat (tag GCMRegistrar):

    GCMRegistrar        Setting the name of retry receiver class to com.example.oldpackage.MyBroadcastReceiver
    

    验证广播接收机的包/类的名称是正确的。

    Verify that the package/class name of the broadcast receiver is correct.

    • 检查LogCat中的错误。
    • 尝试重新启动设备/仿真器。
    • 尝试卸载的设备/仿真器中的应用程序。
    • 尝试重新启动Eclipse的。
    • 尝试清理并重新构建项目。
    • 尝试更新ADT(下拉菜单→窗口→Android的SDK管理器→安装包)。
    • 尝试更新的Eclipse(下拉菜单→帮助→检查更新)。
    • 尝试重新启动您的计算机。
    • 在获得良好的睡眠,或祈祷。

    这篇关于GCM定制的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆