Reputation: 75
package nath.prem.com.premgcmproject;
import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService;
/** * Created by prem on 22/7/16. */ public class GCMTokenRefreshListenerService extends InstanceIDListenerService {
//If the token is changed registering the device again @Override public void onTokenRefresh() { Intent intent = new Intent(this, GCMRegistrationIntentService.class); startService(intent); } }
Error while getting GCM token in client side
FATAL EXCEPTION: IntentService[]
java.lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat at com.google.android.gms.iid.zzd.zzdL(Unknown Source)
at
com.google.android.gms.iid.zzd.(Unknown Source) at com.google.android.gms.iid.zzd.(Unknown Source) at com.google.android.gms.iid.InstanceID.zza(Unknown Source) at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
this is the error I am getting while getting gcm token
public class GCMRegistrationIntentService extends IntentService { //Constants for success and errors public static final String REGISTRATION_SUCCESS = "RegistrationSuccess"; public static final String REGISTRATION_ERROR = "RegistrationError"; //Class constructor public GCMRegistrationIntentService() { super(""); } @Override protected void onHandleIntent(Intent intent) { //Registering gcm to the device registerGCM(); } private void registerGCM() { //Registration complete intent initially null Intent registrationComplete = null; //Register token is also null //we will get the token on successfull registration String token = null; SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try { //Creating an instanceid InstanceID instanceID = InstanceID.getInstance(this); // // //Getting the token from the instance id token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); //Displaying the token in the log so that we can copy it to send push notification //You can also extend the app by storing the token in to your server Log.w("GCMRegIntentService", "token:" + token); //on registration complete creating intent with success registrationComplete = new Intent(REGISTRATION_SUCCESS); //Putting the token to the intent // registrationComplete.putExtra("token", token); } catch (Exception e) { //If any error occurred Log.w("GCMRegIntentService", "Registration error"); registrationComplete = new Intent(REGISTRATION_ERROR); } //Sending the broadcast that registration is completed LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); } }
this is the java code of that class
this is my menifest file in the project......But I cannot see any of the logs in RegistrationIntentService:
Upvotes: 1
Views: 1100
Reputation: 1283
Try this,
Add compile 'com.google.android.gms:play-services-gcm:9.0.2'
dependency in your build.gradle
private void registerToGCM() {
new AsyncTask<String, String, String>() {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage(getString(R.string.gcm_register_message));
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String registryId = null;
try {
InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
registryId = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} catch (Exception ex) {
Log.e("MAINACIVITY", "gcm register Error " + ex.toString());
}
return registryId;
}
@Override
protected void onPostExecute(String registeredId) {
super.onPostExecute(registeredId);
progressDialog.dismiss();
// perform action here
}
}.execute("");
}
Upvotes: 0