2013-07-01 123 views
1

我正在使用使用GoogleCloudMessaging類的gcm描述here的新方式。 但10次中的9次給我SERVICE_NOT_AVAILABLE錯誤。只有一次它註冊一個設備。使用GoogleCloudMessaging的Android gcm客戶端

07-01 14:24:55.907 2074-2090/com.deveyes.carrefour   W/System.err:     java.io.IOException: SERVICE_NOT_AVAILABLE 

07-01 14:24:56.037 2074-2090/com.deveyes.carrefour   W/System.err: at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source) 

我的代碼是我的活動裏面如下:

..... 
    gcmRegId = gcm.register(CarrefourConfig.GCM_SENDER_ID); 
.....              

後來我才知道,我需要使用自動重試。我已閱讀this,但此文檔似乎已舊。與新方法一樣,我們不再使用IntentService。或者我們呢?在BroadcastReciever裏面我可以得到消息並通知用戶。

我在做什麼錯在這裏?

編輯:

我的清單:

<?xml version="1.0" encoding="utf-8"?> 

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


<supports-screens 
     android:xlargeScreens="false" 
/> 


<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<uses-feature android:name="android.hardware.location" android:required="true" /> 
<uses-feature android:name="android.hardware.location.gps" android:required="false" /> 

<permission 
     android:name="com.deveyes.carrefour.android.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 


<uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

<uses-permission android:name="com.deveyes.carrefour.android.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 


<permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" 
      android:protectionLevel="signature" /> 
<uses-permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" /> 

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 


<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.Carrefour" 
     android:name=".CarrefourApplication"> 

    <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyCKznwauzk_rNdKf7Qw0rjpmf4AWchCRwI" /> 

    <activity 
     android:name=".ui.MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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


    <receiver 
      android:name=".gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.deveyes.carrefour" /> 
     </intent-filter> 
    </receiver> 

    <!--service android:name=".services.CarrefourLocationService" /--> 


</application> 

我的MainActivity:

private void registerBackground() { 

    new AsyncTask(){ 

     @Override 
     protected Object doInBackground(Object[] objects) { 

      try { 
       if(gcm == null){ 
        gcm = GoogleCloudMessaging.getInstance(context); 
       } 
       gcmRegId = gcm.register(CarrefourConfig.GCM_SENDER_ID); 

       Log.i(TAG,"regid:"+gcmRegId); 

       setRegistrationId(context,gcmRegId); 
       ServerUtilities.register(context,gcmRegId); 


      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    }.execute(null,null,null); 
} 
+0

請包含更多代碼(您的清單+您調用'gcm.register'的方法)。如果你在主線程中調用'gcm.register',這是一個錯誤。 – Eran

+0

請檢查您的發件人ID –

+0

添加的代碼,我的發件人是正確的。有時會註冊設備,有時不會。 – Shahriar

回答

2

這裏的問題:

你應該在下面的所有3個地方使用相同的包名,但是你在其他情況下,使用在某些情況下com.deveyes.carrefour.androidcom.deveyes.carrefour。哎

<permission android:name="com.deveyes.carrefour.permission.C2D_MESSAGE" 
      android:protectionLevel="signature" /> 
<uses-permission android:name="com.deveyes.carrefour.permission.C2D_MESSAGE" /> 
... 
    <receiver 
      android:name=".gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.deveyes.carrefour" /> 
     </intent-filter> 
    </receiver> 
+0

感謝您糾正錯誤。但仍顯示SERVICE_NOT_AVAILABLE。它工作1-2次,在其他時間都失敗。谷歌方面的服務器問題? – Shahriar

+0

@Shahriar我認爲這會解決你的問題。當我在清單的權限部分中輸入錯誤的軟件包名稱時,我遇到了類似的問題。在大多數情況下,註冊將失敗。當我在清單中發現錯誤並修復它時,問題就消失了。 – Eran

+0

是的,我仍然試圖找出問題:( – Shahriar

1

在GCM罐子應該有一些所謂的GCMRegistrar你應該使用這種方法來註冊GCM:

<permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" 
      android:protectionLevel="signature" /> 
<uses-permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" /> 
... 
    <receiver 
      android:name=".gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.deveyes.carrefour" /> 
     </intent-filter> 
    </receiver> 

如果你的包名是com.deveyes.carrefour,清單改變。以下是代碼,嘗試

GCMRegistrar.checkDevice(this); 
GCMRegistrar.checkManifest(this); 
registerReceiver(mHandleMessageReceiver, new IntentFilter(
       DISPLAY_MESSAGE_ACTION)); 

     final String regId = GCMRegistrar.getRegistrationId(this); 
     if (regId.equals("")) { 
      GCMRegistrar.register(this, SENDER_ID); //this is where it registers 
     } else { 

      if (GCMRegistrar.isRegisteredOnServer(this)) { 
        Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show(); 
      } 

我希望它爲你工作

+0

這是舊的(現在已棄用) API。OP想使用新的'GoogleCloudMessaging'類註冊到GCM。 – Eran

+0

確定我的錯誤!!!! –

+0

現在應該使用什麼? –

0

我有同樣的問題。

9/20拋出SERVICE_NOT_AVAILABLE異常。顯然這是Google的一個漏洞。適用於某些設備(S3),不適用於其他設備(LG L5)。

檢查Here

我認爲要做的事情是暫時使用舊的API,直到問題解決。