Created
November 29, 2015 12:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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