1

我正在關注this tutorial frrom androidHive以在我的應用中整合GCM。無法在GCM中註冊設備

在我之前的項目中,我通過關注此博客來整合GCM。這工作正常。

但是,在我的新項目中,這是行不通的。我在我的服務器中沒有數據。這兩個項目之間的唯一區別是:在我以前的項目中,所有與GCM相關的類都在我的項目的默認包中。但在我的新項目中,與GCM相關的類位於'com.gcm'包中,我從另一個包的活動(這是我的應用程序的默認包)調用它們。我有跟蹤代碼執行

//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」在每一個類別和每一個可能的地方:(但他們沒有任何打印。

(使用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問題的答案時,解決方案是在答案問題。

+0

@Eran:謝謝。這是我正在尋找的答案。你能否請你發表評論作爲答案,以便我能接受你的答案? – Shoshi

回答

0

我建議遵循谷歌GCM網站上的說明,而不是:Google GCM Client Dev Guide

您應該從谷歌的GCMBroadcastReceiver,或WakefulBroadcastReceiver擴展廣播接收器。在這個擴展接收器中,定義GcmIntentService類的位置。如果您的GcmIntentService不在您的主包路徑中,那麼您需要輸入包路徑。