无法使用GCM注册设备

时间:2013-09-01 14:15:42

标签: android broadcastreceiver android-service google-cloud-messaging

我正在跟随this tutorial frrom androidHive在我的应用中投射GCM。

在我之前的项目中,我通过关注此博客集成了GCM。这很好。

但是,在我的新项目中,这不起作用。我的服务器中没有数据。这两个项目的唯一区别是:在我以前的项目中,所有与GCM相关的类都在我项目的默认包中。但在我的新项目中,GCM相关的类在'com.gcm'包中,我从另一个包的活动(这是我的应用程序的默认包)中调用它们。我跟踪(使用Log.e)代码执行到

//in -- MainActivity.java
// Check if regid already presents    
if (regId.equals("")) {    
    // Registration is not present, register now with GCM    
    GCMRegistrar.register(this, SENDER_ID);
    // after this nothing is happening
} else {

我在每个班级和每个可能的地方都有'Log.e':(但他们没有打印任何东西。

我认为服务和接收器设置不正确。 所以,我想我必须改变清单。但我不知道该改变什么。有人可以帮助我吗?

来自博客的清单:

In the following code replace all 'com.androidhive.pushnotifications'
with your android package name.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidhive.pushnotifications"
    android:versionCode="1"
    android:versionName="1.0" >

    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <!-- GCM connects to Internet Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- 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. -->
    <permission
        android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
        // i have changed 'com.androidhive.pushnotifications' to 'com.gcm' but getting error.
        // so i have rplace 'com.androidhive.pushnotifications' with my default package name.

    <uses-permission android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" />
    // i have changed 'com.androidhive.pushnotifications' to 'com.gcm' but not woeking

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

    <!-- Network State Permissions to detect Internet status -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- Permission to vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- Main activity. -->
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!-- Register Activity -->
        <activity
            android:name=".RegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main Activity -->
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
        </activity>

        <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.androidhive.pushnotifications" />
                                       // what will be this?
                                       // replace with default package or
                                          'com.gcm' package ?
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" /> 
                // i have change this to 'com.gcm.GCMIntentService'
    </application>

</manifest>

...

<permission
    android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
    // i have changed 'com.androidhive.pushnotifications' to 'com.gcm' but getting error.
    // so i have rplace 'com.androidhive.pushnotifications' with my default package name.

  <uses-permission    
       android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" />
       // i have changed 'com.androidhive.pushnotifications' to 'com.gcm' but not woeking

...

<service android:name=".GCMIntentService" /> 
    // i have change this to 'com.gcm.GCMIntentService'

...

<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.androidhive.pushnotifications" />
                            // what will be this?
                            // replace with default package or
                               'com.gcm' package ?
    </intent-filter>
</receiver>

编辑:(解决方案)

由于伊兰提出了this问题的答案,解决方案就是问题的答案。

1 个答案:

答案 0 :(得分:0)

我建议您按照Google GCM网站上的说明进行操作:Google GCM Client Dev Guide

您应该从Google的GCMBroadcastReceiver或WakefulBroadcastReceiver扩展广播接收器。在此扩展接收器中,定义GcmIntentService类的位置。如果您的GcmIntentService不在主程序包路径中,则需要输入程序包路径。

相关问题