开发者

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

开发者 https://www.devze.com 2023-02-08 11:42 出处:网络
notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); note = new Notification(android.R.drawable.btn_star_big_on, \"Ticker Text\", System.currentTimeMillis()
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 Inten开发者_如何学运维t();
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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消