首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >instanceID.GetToken()给出异常

instanceID.GetToken()给出异常
EN

Stack Overflow用户
提问于 2016-05-30 11:52:51
回答 1查看 1.5K关注 0票数 0

下面是GCM的代码,得到token...gives异常。

代码语言:javascript
复制
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;
    }
}

异常

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () 0x0000c的Java.IO.IOException: SERVICE_NOT_AVAILABLE

完全例外

{ 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 )中的SERVICE_NOT_AVAILABLE在/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作用域)中,LeaveApplication.Droid.RegistrationIntentService.OnHandleIntent (Android.Content.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(未知源) at com.google.android.gms.iid.zzc.zza(未知源)在com.google.android.gms.iid.InstanceID.zzc(Unknown来源)在com.google.android.gms.iid.InstanceID.getToken(Unknown来源)在md54d5d90974a2844b8ac95dbb0c513d773.RegistrationIntentService.n_onHandleIntent(Native方法)在md54d5d90974a2844b8ac95dbb0c513d773.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:36),在android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65),在android.os.Looper.loop(活套)。android.os.HandlerThread.run(HandlerThread.java:61) }

我的安卓manifest.xml

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2016-05-30 20:23:17

SERVICE_NOT_AVAILABLE是库的一个有效错误:

设备无法读取响应,或出现服务器错误。应用程序应该稍后使用指数退避和重试重试请求(每次失败都会增加重试之前的延迟)。

请参阅:https://developers.google.com/android/reference/com/google/android/gms/iid/InstanceID.html

当库由于设备条件(通常没有连接/无法到达google服务器)或其他服务器问题而无法获得令牌时,就会发生此错误。

在这种情况下,实现重试逻辑是您的责任。

注:2016年谷歌I/O 发布了Firebase消息传递。

请参阅:https://firebase.google.com/docs/cloud-messaging

引用该网站的话:

这是GCM的新版本。它继承了可靠和可伸缩的GCM基础设施,以及新的功能。请参阅FAQ以了解更多信息。如果您正在将消息传递集成到新应用程序中,请从FCM开始。强烈建议GCM用户升级到FCM,以便从今天和将来的新FCM功能中获益。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37525041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档