android - GCMIntentService 的 onRegistered() 从未调用过

标签 android google-cloud-messaging

我正在尝试在 GCM 上注册我的应用程序,但我不知道为什么我的应用程序从未注册过。 GCMRegistrar.register(this, SENDER_ID); 被调用,但我的 GCMIntentService 中的 onRegistered() 从未被调用。我不知道为什么

这是我的日志

01-17 11:03:00.015: D/GCMRegistrar(3509): resetting backoff for com.abc.xyz.ui.activity
01-17 11:03:03.210: V/GCMRegistrar(3509): Registering app com.abc.xyz.ui.activity of senders 964256581311
01-17 11:03:06.070: V/GCMBroadcastReceiver(3509): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-17 11:03:06.070: V/GCMBroadcastReceiver(3509): GCM IntentService class: com.abc.xyz.ui.activity.GCMIntentService
01-17 11:03:06.070: V/GCMBaseIntentService(3509): Acquiring wakelock

这是我的完整 list

    <?xml version="1.0" encoding="utf-8"?>
<manifest package="com.abc.xyz.ui.activity"
    android:versionCode="1"
    android:versionName="1.5.6" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk 
        android:minSdkVersion="11" android:targetSdkVersion="16"/>
    <uses-feature 
        android:name="android.hardware.usb.host"/>

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />

    <uses-permission 
        android:name="android.permission.INTERNET" />
    <uses-permission 
        android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission 
        android:name="android.permission.GET_TASKS" />
    <uses-permission 
        android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission
        android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission 
        android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission 
        android:name="android.permission.CALL_PHONE" /> 
    <uses-permission 
        android:name="android.permission.BLUETOOTH" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <!--
     Creates a custom permission so only this app can receive its messages.

     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
           where PACKAGE is the application's package name.
    -->
    <uses-permission
        android:name="com.abc.xyz.ui.activity.permission.C2D_MESSAGE" />   
    <permission
        android:name="com.abc.xyz.ui.activity.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light" 
        android:name="com.abc.xyz.MyApplication" 
        android:allowBackup="false">
        <activity
            android:name=".StartupActivity"
            android:noHistory="true"
            android:label="@string/title_startup_screen" 
            android:configChanges="orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
            android:label="@string/title_login_screen"
            android:configChanges="orientation">
        </activity>
        //my other activity defination

        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.

          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            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="com.abc.xyz.ui.activity" />
            </intent-filter>
        </receiver>

        <!--
          Application-specific subclass of GCMBaseIntentService that will
          handle received messages.

          By default, it must be named .GCMIntentService, unless the
          application uses a custom BroadcastReceiver that redefines its name.
        -->
        <service android:name="com.abc.xyz.ui.activity.GCMIntentService" />

    </application>
</manifest>

我不知道哪里出了问题,也不知道为什么永远不会调用 onRegistered()。任何帮助将不胜感激。

最佳答案

我在另一篇文章中弄清楚了问题所在。我的 GCMIntentService 类在我的 list 中定义为

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

因为我不想将此类放在根包中,而是放在 MY_PACKAGE.gcm 中。这似乎会导致问题,正如文档所述,

添加以下 Intent 服务

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

所以当我移动到根包时它起作用了!还有另一种方法可以将 GCMIntentServiceSubclass 放在任何你想要的地方,并以不同的方式命名。您应该将 GCMBroadcastReceiver 子类化并在 list 和子类中进行更改,如其他 post 所示。 .

关于android - GCMIntentService 的 onRegistered() 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14397202/

相关文章:

Android:撤消应用程序数据目录上的 chmod

android - 从 Activity 调用 BroadcastReceiver

android - 同一个第 3 方应用程序服务器是否可以同时满足 CCS 和 GCM 的需求,或者我应该创建两个版本的第 3 方应用程序?

android - PushApps Android 推送通知示例

java - GCM 在 2.3.6 中没有收到消息,但在 4.1.2 中没问题

java - 如何在Google AppEngine中实现GCM?

android - 从其他应用程序打开聊天屏幕 Skype

java - android 和 java 中的 SQLite 数据库

android - 通过 Android GMail 应用程序发送 PNG 附件

android - 使用XMPP在android中用户离线时向用户发送通知