How to open a specific activity based on a notification

I am currently working on a reminder application in which the user receives a notification with the name of the reminder and then is redirected to an operation containing the text of the reminder in detail. However, I can only redirect to the same activity every time. I am using this code:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); notification.setLatestEventInfo(this, title, text, contentIntent); 

So this is a redirect to MainActivity when you click on a notification. I would like to redirect to a separate screen, and then based on the key value display the text of this action. How to achieve this?

thanks

0
source share
3 answers

Just change the PendingIntent with another Activity and / or add the extra Intent information you use to create the PendingIntent :

 Intent launchIntent = new Intent(this, AnotherActivity.class) launchIntent.putExtra("myKey", "myValue"); //.... PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent , 0); notification.setLatestEventInfo(this, title, text, contentIntent); 

And than in you Activity onCreate() :

 //... getIntent().getStringExtra("myKey") //do your stuff.. 
+2
source

Just convey the meaning in intention. eg.

 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); contentIntent.putExtra("phone", value); contentIntent.putExtra("name", value); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
0
source

The question has already been accepted and accepted, but here is another method:

 int mId = 1; PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this) .setContentIntent(pendingIntent) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Title") .setContentText("Content") .setDefaults(Notification.DEFAULT_ALL) .setAutoCancel(true) .setNumber(1); NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(mId, builder.build()); 
0
source

Source: https://habr.com/ru/post/1234969/


All Articles