GCM(Google Cloud Messaging) registration token is a kind of id given by the GCM connection server to user/client that allow user to receive messages. To get registration token first we have to generate a unique number, it can be created while configure your API project, now given as Project number in Google Developer Console.
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
try {
InstanceID instanceID = InstanceID.getInstance(context);
token = instanceID.getToken(context.getResources().getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} catch (Exception e) {
Log.d("TAG", "Failed to get token", e);
}
return token;
}
@Override
protected void onPostExecute(String token) {
Log.i("GCM Registration Token:", token);
}
};
task.execute();
0 Comment(s)