Jase Whatson
Jase Whatson

Reputation: 4207

I have a notification which has a attached intent, how do I pass data to the intents activity?

notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
note = new Notification(android.R.drawable.btn_star_big_on, "Ticker Text", System.currentTimeMillis() );

note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.FLAG_AUTO_CANCEL;

Intent notificationIntent = new Intent(ctx, com.mindfsck.PossAff.MainActivity.class);
//Intent notificationIntent = new Intent();
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);

note.setLatestEventInfo(ctx, "contentTitle", "contentText", contentIntent);

note.number = 1;  //Just created notification so number=1. Remove this line if you dont want numbers

notificationManager.notify(notif_ID,note);

I have a notification with a intent. When you click the notification it launches the activity however in MainActivity I have no idea how to consume the intent. I also assume once I know how to read the Intent in my Main Activity I can use putExtra() to pass data.

Thanks

Upvotes: 0

Views: 247

Answers (2)

Saikat Biswas
Saikat Biswas

Reputation: 114

The extras you set in the intent have to be retrieved by the getIntent().getExtras() the savedInsanceState is more about retrieving the state.

Upvotes: 0

user432209
user432209

Reputation: 20167

In the Activity you started you can use:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    //read extras (extras.getString/getBoolean/etc)
}

...and yes, you will need to use putExtra to pass the data to the Intent.

Upvotes: 1

Related Questions