Issue with fetching extras from intent:

User3

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);
}
Anup Dasari

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

can not get extras from intent

From Dev

NullPointerException while retrieving Extras from Intent

From Dev

Passing intent extras from a google cloud message receiver to activity is null

From Dev

How do I access the extras from an intent in RemoteViewsService?

From Dev

Cant get Extras from intent when application is in background

From Dev

Unmarshalling extras from intent throws an exception but only during onActivityReenter

From Dev

Intent extras is not updating

From Dev

Missing 'extras' data in intent

From Dev

Intent extras not changed

From Dev

Intent extras not received

From Dev

get notification intent extras

From Dev

Android Intent extras

From Dev

NullPointerException on Intent Extras

From Dev

Override intent extras

From Dev

Issue in fetching iPhone contacts from iOS SDK

From Dev

Issue while fetching the title from an ahref

From Dev

Issue with fetching data from excel (Using Java)

From Dev

Fetching data issue from mysql database in this code

From Dev

issue to pass arraylist from an intent to another

From Java

PendingIntent does not send Intent extras

From Dev

List of all extras applicable to an intent

From Dev

Android - Dynamically set intent Extras

From Dev

Unable to retrieve new intent extras

From Dev

Intent extras null on configuration change

From Dev

List of all extras applicable to an intent

From Dev

SendBroadcast Intent Has Null Extras

From Dev

Unable to receive extras in Android Intent

From Dev

Get Extras (variables) out of an Intent

From Dev

Can't receive Intent.extras() from Camera after making a photo