1

我正在按照本教程从 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>

编辑:(解决方案)

正如 Eran 建议的 这个问题的答案,解决方案就在问题的答案中。

4

1 回答 1

0

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

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

于 2013-09-01T16:46:02.453 回答