Skip to content

Instantly share code, notes, and snippets.

@Artur-Sulej
Created November 29, 2015 12:55

Revisions

  1. Artur-Sulej created this gist Nov 29, 2015.
    27 changes: 27 additions & 0 deletions GcmUtil.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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;
    }
    }
    }