I have the following broadcast receiver:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive");
final String token1 = intent.getStringExtra("token");
System.out.println("onReceive" + token1);
Bundle extras = getIntent().getExtras();
if (extras != null) {
final String token = intent.getStringExtra("token");
System.out.println("Token fetched");
new PostLoginWithGCM(cleanKey, token).execute();
} else {
System.out.println("Token null");
}
}
};
The output is:
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: onReceive
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: onReceivefy6gmbUODWk:APA91bHf3PrCQmzZXnfR5LU40C7Mu1jhHenX8SvYR2OvQR6A3npXKa4NF8J-eZtHoxO7QAbTe_S94L8IttuU7ZrT5S97mucJb6EmmF92y3Hi60lrGb6cBfjDHj9fYDfaL8chion-Uyh5
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: Token null
So the first System.out onReceive is working fine.
Second System.out gives me the token value which I pass with the Intent.
Finally I check if my extras are null or not and to my amazement it says that my extras are null, while as I just printed the data before the null check.
This is the method responsible for generating me a token and the local broadcast:
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
InstanceID instanceID = InstanceID.getInstance(this);
token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
subscribeTopics(token);
sharedPreferences.edit().putBoolean(SessionManager.SENT_TOKEN_TO_SERVER, true).apply();
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
sharedPreferences.edit().putBoolean(SessionManager.SENT_TOKEN_TO_SERVER, false).apply();
}
Intent registrationComplete = new Intent(SessionManager.REGISTRATION_COMPLETE);
System.out.println("token before assignment" + token);
registrationComplete.putExtra("token", token.toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
In onReceive method you already have an intent as parameter. so instead of this
Bundle extras = getIntent().getExtras();
Try this
Bundle extras = intent.getExtras();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments