Unable to get token on API level 17

2019-02-20 11:42发布

问题:

I am running these commands in order to get a token:

  InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
  String token = instanceID.getToken(Config.GOOGLE_PROJECT_ID,
                             GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

But getting this exception:

java.io.IOException: MISSING_INSTANCEID_SERVICE
            at com.google.android.gms.iid.zzc.zza(Unknown Source)
            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 autofocus.com.autofocus.activity.ItemListActivity$4.doInBackground(ItemListActivity.java:408)
            at autofocus.com.autofocus.activity.ItemListActivity$4.doInBackground(ItemListActivity.java:398)

Any idea what I am doing wrong?

回答1:

According to official docs:

https://developers.google.com/android/reference/com/google/android/gms/iid/InstanceID#ERROR_MISSING_INSTANCEID_SERVICE

you get this error because

Tokens can't be generated. Only devices with Google Play are supported.

Testing on emulator without Google Play Services enabled/installed?



回答2:

You need to install the gapps of the version of android in your emulator, if use the genymotion you need to install before the gapps di Genymotion-ARM-Translation_v1.1 after that, probably will work