2013-03-21 41 views
1

我能夠在第一次在Samsung Galaxy ace2中運行我的應用程序。現在,我正在跟隨GCM註冊失敗的錯誤。我在模擬器中工作正常,但不在該設備中工作。以下是錯誤和GCMIntentService.javaSamsung Galaxy ace2中的GCM註冊錯誤,但在仿真器中工作

錯誤:

03-21 09:25:33.110: V/GCMBaseIntentService(6018): Acquiring wakelock 
03-21 09:25:33.120: V/GCMBaseIntentService(6018): Intent service name: GCMIntentService-1089764589011-11 
03-21 09:25:33.130: D/GCMBaseIntentService(6018): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null 
03-21 09:25:33.130: D/GCMBaseIntentService(6018): Registration error: SERVICE_NOT_AVAILABLE 
03-21 09:25:33.130: I/GCMIntentService(6018): Received recoverable error: SERVICE_NOT_AVAILABLE 
03-21 09:25:33.130: D/GCMBaseIntentService(6018): Scheduling registration retry, backoff = 98657 (96000) 
03-21 09:25:33.200: V/GCMBaseIntentService(6018): Releasing wakelock 
03-21 09:26:42.950: D/dalvikvm(6018): GC_CONCURRENT freed 354K, 48% free 3310K/6279K, external 630K/1286K, paused 7ms+9ms 
03-21 09:27:11.800: V/GCMBroadcastReceiver(6018): onReceive: com.google.android.gcm.intent.RETRY 
03-21 09:27:11.800: V/GCMBroadcastReceiver(6018): GCM IntentService class: com.dorji.finalproject.GCMIntentService 
03-21 09:27:11.800: V/GCMBaseIntentService(6018): Acquiring wakelock 
03-21 09:27:11.830: V/GCMBaseIntentService(6018): Intent service name: GCMIntentService-1089764589011-12 
03-21 09:27:11.840: V/GCMRegistrar(6018): Registering app com.dorji.finalproject of senders 1089764589011 
03-21 09:27:11.840: V/GCMBaseIntentService(6018): Releasing wakelock 
03-21 09:27:12.010: V/GCMBroadcastReceiver(6018): onReceive: com.google.android.c2dm.intent.REGISTRATION 
03-21 09:27:12.010: V/GCMBroadcastReceiver(6018): GCM IntentService class: com.dorji.finalproject.GCMIntentService 
03-21 09:27:12.010: V/GCMBaseIntentService(6018): Acquiring wakelock 
03-21 09:27:12.020: V/GCMBaseIntentService(6018): Intent service name: GCMIntentService-1089764589011-13 
03-21 09:27:12.020: D/GCMBaseIntentService(6018): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null 
03-21 09:27:12.020: D/GCMBaseIntentService(6018): Registration error: SERVICE_NOT_AVAILABLE 
03-21 09:27:12.020: I/GCMIntentService(6018): Received recoverable error: SERVICE_NOT_AVAILABLE 
03-21 09:27:12.020: D/GCMBaseIntentService(6018): Scheduling registration retry, backoff = 105051 (192000) 
03-21 09:27:12.070: V/GCMBaseIntentService(6018): Releasing wakelock 

GCMIntentService.java

package com.dorji.finalproject; 

import static com.dorji.finalproject.CommonUtilities.SENDER_ID; 
import static com.dorji.finalproject.CommonUtilities.displayMessage; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

import com.dorji.finalproject.R; 
import com.google.android.gcm.GCMBaseIntentService; 

public class GCMIntentService extends GCMBaseIntentService { 

    private static final String TAG = "GCMIntentService"; 

    public GCMIntentService() { 
     super(SENDER_ID); 
    } 

    /** 
    * Method called on device registered 
    **/ 
    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
     displayMessage(context, "Your device registred with GCM"); 
     ServerUtilities.register(context, registrationId); 
    } 

    /** 
    * Method called on device un registred 
    * */ 
    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     Log.i(TAG, "Device unregistered"); 
     displayMessage(context, getString(R.string.gcm_unregistered)); 
     ServerUtilities.unregister(context, registrationId); 
    } 

    /** 
    * Method called on Receiving a new message 
    * */ 
    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "Received message"); 
     String message = intent.getExtras().getString("message"); 

     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 

    } 

    /** 
    * Method called on receiving a deleted message 
    * */ 
    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     Log.i(TAG, "Received deleted messages notification"); 
     String message = getString(R.string.gcm_deleted, total); 
     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    /** 
    * Method called on Error 
    * */ 
    @Override 
    public void onError(Context context, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
     displayMessage(context, getString(R.string.gcm_error, errorId)); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     // log message 
     Log.i(TAG, "Received recoverable error: " + errorId); 
     displayMessage(context, getString(R.string.gcm_recoverable_error, 
       errorId)); 
     return super.onRecoverableError(context, errorId); 
    } 

    /** 
    * Issues a notification to inform the user that server has sent a message. 
    */ 
    private static void generateNotification(Context context, String message) { 
     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3"); 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification);  

    } 

} 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.dorji.finalproject" 
    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" /> 

    <supports-screens android:largeScreens="true" 
     android:normalScreens="true" android:smallScreens="true" 
     android:resizeable="true" android:anyDensity="true" /> 

    <!-- 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.dorji.finalproject.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.dorji.finalproject.permission.C2D_MESSAGE" /> 

    <!-- 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:debuggable="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <!-- Register Activity --> 
     <activity 
      android:name="com.dorji.finalproject.LoginLayout" 
      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="com.dorji.finalproject.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.dorji.finalproject" /> 
      </intent-filter> 
     </receiver> 

     <service android:name="com.dorji.finalproject.GCMIntentService" /> 
    </application> 

</manifest> 
+0

您是否在Ace2中設置了Google帳戶? GCM需要它。 – 2013-03-21 09:34:58

+0

@ol_v_er,非常感謝,我會嘗試 – 2013-03-21 11:54:30

+0

@Spontifixus,感謝您的編輯。 – 2013-03-21 11:56:08

回答

1

即誤差在Google's GCM docs定義如下:

public static final String ERROR_SERVICE_NOT_AVAILABLE

The device can't read the response, or there was a 500/503 from the server that can be retried later. The application should use exponential back off and retry.

所以看起來好像它可能是一個暫時的錯誤,我會再試幾次,看看會發生什麼。

而且屁股@ol_v_er提到你需要確保你的設備符合GCM要求在GCM docs上市:

  • It requires devices running Android 2.2 or higher that also have the Google Play Store application installed, or or an emulator running Android 2.2 with Google APIs. However, you are not limited to deploying your Android applications through Google Play Store.
  • It uses an existing connection for Google services. For pre-3.0 devices, this requires users to set up their Google account on their mobile devices. A Google account is not a requirement on devices running Android 4.0.4 or higher.
+0

非常感謝您的回覆。我認爲我的設備符合GCM要求。首先它運行良好,但後來出現這個錯誤。是的,我嘗試了很多,認爲這是暫時的。謝謝 – 2013-03-21 16:16:13

0

如果你因爲沒有reeason此消息SERVICE_NOT_AVAILABLE並沒有計算器的回答您解決問題可能會被讀取,因爲所需要的只是耐心。所以塞爾茜的答案爲我工作。

在我的應用程序正常工作後,它突然開始生成SERVICE_NOT_AVAILABLE消息並顯示註冊ID爲空。然後我嘗試了兩個不同的模擬器上的應用程序,它在兩者上都工作正常。

我嘗試了各種stackoverflow帖子中的所有建議都無濟於事。我整晚都在設備上運行應用程序,並用SERVICE_NOT_AVAILABLE消息填充了半個屏幕。

我卸載了應用程序並關閉了設備並退出睡覺。第二天早上,我打開安裝的連接並再次運行該應用程序,它完美地工作。

奇怪的是,該設備已註冊爲GCM服務器的新設備。我的應用程序服務器還添加了另一個設備。不幸的是,我的應用程序服務器是一個枯燥的骨頭,沒有持久性或日誌記錄,所以我無法進一步調查。

有沒有人遇到過這種現象?

相關問題