'intent.putExtra() in pending intent not working

I am passing a pending intent through alarmreceiver, from a service class. But, after the pendingIntent fires, the intent.putExtra() information is not being received by the broadcastreceiver class. Here is my code for firing the pendingIntent

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

The alarm receiver class is below

public String msg, phonen;

@Override
public void  onReceive(Context context, Intent intent){
    Bundle extras = intent.getExtras();
    msg = extras.getString("msg");
    phonen = extras.getString("phone");

    Log.d("onReceive", "About to execute MyTask");
    Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
}

The msg information in toast, that is being received from pending intent, is not being shown. Instead, a blank toast is shown.



Solution 1:[1]

Try this

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);



PendingIntent pendingIntent = PendingIntent.getBroadcast(
    getApplicationContext(),
    id, 
    aint,
    // as stated in the comments, this flag is important!
    PendingIntent.FLAG_UPDATE_CURRENT);

Solution 2:[2]

Remember to put an unique id in the PendingIntent constructor, or you could get some weird thing when you try to get the putExtra values.

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
           getApplicationContext(), 
           UUID.randomUUID().hashCode(), 
           aint, 
           PendingIntent.FLAG_UPDATE_CURRENT
    );

Solution 3:[3]

You have to use getStringExtra() and be sure the String are not null:

     Intent intent = getIntent();    
     msg = intent.getStringExtra("msg");
     phonen = intent.getStringExtra("phone");

     if(msg!=null){
      Toast.makeText(context,msg,
        Toast.LENGTH_LONG).show();
     }

and reverse Your putExtras before PendingIntent:

   Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
   aint.putExtra("msg", msg);
   aint.putExtra("phone", phone);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);

Solution 4:[4]

This is because you are first initialize Intent and add to PendingIntent. After that you are adding info in intent. You should add info to intent then add this intent to PendingIntent.

Solution 5:[5]

 final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);

    //put in extra string into my intent
    //tell the clock that the user pressed the "alarm on button"
    my_intent.putExtra("extra" , 1);
    int state = my_intent.getExtras().getInt("extra");
    Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);

I had the same problem and I found out that you should put the putExtra before you involve the Intent

Solution 6:[6]

In my case i was missing PendingIntent.FLAG_UPDATE_CURRENT as flag.

Solution 7:[7]

Please Use FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 schlenger
Solution 2 Hal Cheung
Solution 3 Opiatefuchs
Solution 4 Haris Qurashi
Solution 5 Dharman
Solution 6 gcantoni
Solution 7 Hridoy Krisna Das