Skip to content

Instantly share code, notes, and snippets.

@Artur-Sulej
Created November 29, 2015 12:55
Show Gist options
  • Save Artur-Sulej/cbfd26ae879a72b2b27d to your computer and use it in GitHub Desktop.
Save Artur-Sulej/cbfd26ae879a72b2b27d to your computer and use it in GitHub Desktop.
Utility class which allows you to synchronously get GCM token on Android.
public class GcmUtil {
public static String getToken(Context context) throws IOException {
InstanceID instanceID = InstanceID.getInstance(context);
return instanceID.getToken(context.getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
}
public static String getTokenOnThread(final Context context) {
try {
return new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
return getToken(context);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}.execute().get(30L, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
e.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment