0
votes

Following code for GCM get token...gives exception.

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;
    }
}

Exception

Java.IO.IOException: SERVICE_NOT_AVAILABLE at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c]

Full Exception

{Java.IO.IOException: SERVICE_NOT_AVAILABLE at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /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] in :0 at LeaveApplication.Droid.RegistrationIntentService.OnHandleIntent (Android.Content.Intent intent) [0x0003a] in D:\Workspaces\temp\LeaveApplication\LeaveApplication\LeaveApplication\LeaveApplication.Droid\RegistrationIntentService.cs:36 --- End of managed exception stack trace --- 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(Unknown Source) at com.google.android.gms.iid.InstanceID.getToken(Unknown Source) at md54d5d90974a2844b8ac95dbb0c513d773.RegistrationIntentService.n_onHandleIntent(Native Method) at 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) at android.os.HandlerThread.run(HandlerThread.java:61) }

my 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

1 Answers

1
votes

The SERVICE_NOT_AVAILABLE is a valid error of the library:

The device cannot read the response, or there was a server error. Application should retry the request later using exponential backoff and retry (on each subsequent failure increase delay before retrying).

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

This error happens when then library fails to obtain a token due to device conditions (usually no connectivity / impossible to reach google servers) or other server issues.
In this case is your responsibility to implement a retry-logic.

NOTE: at Google I/O 2016 we released Firebase Cloud Messaging (FCM).
see: https://firebase.google.com/docs/cloud-messaging

Quoting the website:

This is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features. See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

A great benefit of the new library is that we now handle auto-retry of the getToken() automatically, so you don't have to write that logic.