Reputation: 1059
I have created a custom notification. In this notification I have a Button. My problem is when I click the button I can't seem to pass any extra to the new class.
This is the code I am using on the button:
// notification button
Intent switchIntent = new Intent(context, SecondClass.class);
switchIntent.putExtra("passThis", passThisValue);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, switchIntent, 0);
contentView.setOnClickPendingIntent(R.id.button, pendingSwitchIntent);
The class I want the extras:
public class SecondClass extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String id = extras.getString("passThis"); } }
Bundle is allways null. I only posted the part of the code where I think the problem is, if you need the full code I will edit my post.
I am not doing this correctly? How should I pass a value to a class on a Notification Button Event?
Upvotes: 1
Views: 152
Reputation: 722
the above code doesnt pass the data as bundle its just going as intent extra.... you have to call
getIntent().getStringExtra("YOURKEY");
Upvotes: 1
Reputation: 4444
I made a tutorial about this for one of my neighbors in case you want to review it. Here's the link
Upvotes: 0
Reputation: 7358
Try this code
Bundle bundle = new Bundle();
bundle.putString("passThis", passThisValue);
switchIntent.putExtras(bundle);
Upvotes: 1
Reputation: 969
If you want to get the passed Bundle from notification Override an onNewIntent to your activity like the code below. Hope this help.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if(extras!=null) {
String id = extras.getString("passThis");
}
}
Upvotes: 0