Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a browser on notification click in Android?

Tags:

android

This is my code, I want to open a URL when user clicks on notification.

This is the code:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));

Notification myNotification = new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setAutoCancel(false)
                    .setLargeIcon(remote_picture)
                    .setContentTitle(onvan)
                    .setContentIntent(notificationIntent)
                    .setContentText(msg)
                    .setStyle(notiStyle)
                    .build();
notificationManager.notify(1, myNotification);

I got this error : The method setContentIntent(PendingIntent) in the type NotificationCompat.Builder is not applicable for the arguments (Intent)

What am I doing wrong? How can I tell it to open browser when user clicks on the notification?

like image 466
mohamad bagheri Avatar asked Dec 01 '25 05:12

mohamad bagheri


1 Answers

You have to add pending intent to the the notification.

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));

PendingIntent pending = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.setContentIntent(pending);
like image 177
Deepak Goyal Avatar answered Dec 05 '25 16:12

Deepak Goyal