To send an Intent
from a notification with extra data to an activity on Android, you can use the following code:
Intent i = new Intent(this, TargetActivity.class); i.putExtra("key", "value"); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Title") .setContentText("Content") .setContentIntent(pendingIntent) .setAutoCancel(true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(NOTIFICATION_ID, builder.build());
In this example, TargetActivity
is the name of the activity that you want to start when the notification is clicked. The Intent
sets the extra data using the putExtra
method and sets the flags to FLAG_ACTIVITY_NEW_TASK
and FLAG_ACTIVITY_CLEAR_TASK
, which will create a new task and clear any existing tasks when the activity is launched.
The Intent
is then wrapped in a PendingIntent
using the getActivity
method, which allows it to be used as the content intent for the notification.
The notification is built using the NotificationCompat.Builder
class and set with a small icon, title, and text. The content intent is set using the setContentIntent
method, and the notification is set to be automatically canceled when clicked using the setAutoCancel
method.
Finally, the notification is displayed using the NotificationManagerCompat
class.
For more information on using Intents
with notifications in Android, you can refer to the Android documentation.