2013-07-23 17 views
6

我已經創建了一個GCM意圖服務,並且設備正在成功註冊。我也收到了REGISTRATION Intent,但OnMessage或OnRegistered方法沒有被調用。GCM意向服務OnRegistered沒有被調用

在日誌下面我看到。

07-23 06:24:23.542: V/GCMBroadcastReceiver(1168): onReceive: com.google.android.c2dm.intent.REGISTRATION 
07-23 06:24:23.542: V/GCMBroadcastReceiver(1168): GCM IntentService class: com.app.demo.myApp.GCMIntentService 
07-23 06:24:23.581: V/GCMBaseIntentService(1168): Acquiring wakelock 

下面是OnMessage的代碼。

有人可以幫我解釋爲什麼OnRegistered或OnMessage沒有被調用。

public class GCMIntentService extends GCMBaseIntentService { 



    @Override 
    protected void onMessage(Context arg0, Intent arg1) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "registered", Toast.LENGTH_LONG).show(); 
     String device_id = GCMRegistrar.getRegistrationId(this); 
     GSLogger.Log("mess_received", device_id); 
    } 

    @Override 
    protected void onRegistered(Context context, String arg1) { 
     Toast.makeText(context, "registered", Toast.LENGTH_LONG).show(); 
     String device_id = GCMRegistrar.getRegistrationId(this); 
     GSLogger.Log("registered", device_id); 

    } 

} 

清單代碼:

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-configuration android:name="android.permission.GET_ACCOUNTS"/> 
    <uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/> 


    <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="com.app.demo.myApp.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" /> 

接收器代碼在清單:

<receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="com.app.demo.myApp" /> 
      </intent-filter> 
     </receiver> 
     <service android:name="com.app.demo.myApp.GCMIntentService" /> 

產品圖我的包: enter image description here

+0

讓我看看您的manifest.xml代碼 –

+0

更新清單代碼。 因爲我可以看到GCMBroadCastReciver的OnReceived Log,所以我覺得在Manifest中應該沒有問題。 – Raghav

回答

24

的問題是

<service android:name="com.app.demo.myApp.GCMIntentService" /> 

只要給

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

並經中體現你的包名指定將您GCMIntentService的Java文件的文件夾中。

例如,如果您已聲明com.app.demo.myApp作爲應用程序包的名稱

將您在同一文件夾結構GCMIntentService。

+0

當我從serice刪除軟件包名稱時,出現註冊錯誤 註冊錯誤:AUTHENTICATION_FAILED – Raghav

+0

您是否在正確的軟件包位置添加了GCMIntentService? – Nargis

+0

我把它放在src文件夾下的所有其他.java文件中的相同包中。是那個正確的位置? – Raghav

2

我看到一些潛在的問題與你的表現,雖然我不知道他們是否是造成你的問題:

以下是不需要:

<uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/> 

你已經擁有了正確的C2D_MESSAGE權限:

<permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
<uses-permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" /> 

此外,GCM從API級別8的支持,所以你可能不應該指定​​

+1

一些示例項目有兩個權限重複且冗餘。好點,刪除第二個似乎解決了我的問題 – voghDev

2

確保你把你的GCMIntentService放在主文件夾中..不要把它放在任何子文件夾下面,它會起作用。我有同樣的問題,並開始工作。

+0

這就是我發現了,但爲什麼地獄..? – Micro