2016-05-30 94 views
0

以下代码为GCM获取令牌...给出例外。instanceID.GetToken()给出异常

protected override void OnHandleIntent(Intent intent) 
{ 
    try 
    { 
     Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken"); 
     lock (locker) 
     { 
      var instanceID = InstanceID.GetInstance(Application.Context); 


      var token = instanceID.GetToken(
       "<Project number>", GoogleCloudMessaging.InstanceIdScope, null);// exception occurred at this line 

      Log.Info("RegistrationIntentService", "GCM Registration Token: " + token); 
      SendRegistrationToAppServer(token); 
      Subscribe(token); 
     } 
    } 
    catch (Exception e) 
    { 
     Log.Debug("RegistrationIntentService", "Failed to get a registration token"); 
     return; 
    } 
} 

异常

java.io.IOException的:在SERVICE_NOT_AVAILABLE System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000c]

全部异常

{Java.IO.IOException:SERVICE_NO T_AVAILABLE at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000c] /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon .cs:143 at Android.Runtime.JNIEnv.CallObjectMethod(IntPtr jobject,IntPtr jmethod,Android.Runtime.JValue * parms)[0x00064] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid /src/Mono.Android/src/Runtime/JNIEnv.g.cs:195 at Android.Gms.Gcm.Iid.InstanceID.GetToken(System.String authorizedEntity,System.String scope,Android.OS.Bundle extras) [0x00096]:0在 LeaveApplication.Droid.RegistrationIntentService.OnHandleIntent (Android.Content.Intent intent)[0x0003a] D:\ Workspaces \ temp \ LeaveApplication \ LeaveApplication \ LeaveApplication \ LeaveApplication.Droid \ RegistrationIntentService.cs:36 ---管理的异常堆栈跟踪结束--- java .io.IOException:SERVICE_NOT_AVAILABLE at com.google.android.gms.iid.zzc.zzb(Unknown Source)at com.google.android.gms.iid.zzc.zza(Unknown Source)at com.google.android .gms.iid.InstanceID.zzc(来源不明)处 md54d5d90974a2844b8ac95dbb0c513d773.RegistrationIntentService.n_onHandleIntent(本地 法) com.google.android.gms.iid.InstanceID.getToken(来源不明)在 md54d5d90974a2844b8ac95dbb0c513d773.RegistrationIntentService.onHandleIntent (RegistrationIntentService.java:36) at android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:135 )在 android.os.HandlerThread.run(HandlerThread.java:61)}

我的Android的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yourcompany.LeaveApplication" android:installLocation="auto" 
      android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="15" /> 
    <application android:label="XamarinLeaveApp"></application> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.yourcompany.LeaveApplication.permission.C2D_MESSAGE" /> 
    <permission android:name="com.yourcompany.LeaveApplication.permission.C2D_MESSAGE" 
       android:protectionLevel="signature" /> 
    <receiver android:name="com.google.android.gms.gcm.GcmReceiver" 
       android:exported="true" 
       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.yourcompany.LeaveApplication" /> 
    </intent-filter> 
    </receiver> 
</manifest> 

回答

1

SERVICE_NOT_AVAILABLE是图书馆的一个有效的错误:

设备无法读取响应,或者出现服务器错误。 应用程序应在稍后使用指数回退 重试请求并重试(重试之前的每个后续故障增加延迟)。

见:https://developers.google.com/android/reference/com/google/android/gms/iid/InstanceID.html

时则库无法获得令牌由于设备的条件(通常不连接/不可能达到谷歌服务器)或其他服务器问题此错误发生的情况。
在这种情况下,您有责任实施重试逻辑。

注意:在谷歌I/O 2016我们发布了Firebase云消息传递(FCM)。
看到:https://firebase.google.com/docs/cloud-messaging

引述网站:

这是GCM的新版本。它继承了可靠和可扩展的GCM基础设施以及新功能。查看常见问题以了解更多信息。如果 您正在将消息集成到新应用程序中,请从FCM开始。强烈建议GCM用户 升级到FCM,以便从当前和未来的 新增FCM功能中受益。

新库的一个很大的好处是我们现在自动处理getToken()的自动重试,因此您不必编写该逻辑。

+0

这是我的错误,互联网不在设备上工作,这是我从我的组织。 –