Hey all long time no see!! As i started Internet Marketing i quited to code but somehow everything is about coding... :D
So i am trying to come up with new method but i will talk about that in the next thread :)
so I am trying to create push notification and when i click on it to open an URL in a browser, i am using PendingIntent but i have truble doing that.
Here is the code i have

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        lateinit var notificationManager : NotificationManager
        lateinit var notificationChannel : NotificationChannel
        lateinit var builder : Notification.Builder
        val channelId = "com.example.myapplication"
        val desc = "Test Notification"

        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val intent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        place_order.setOnClickListener {
            //startActivity(Intent(this, AboutMe::class.java))
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationChannel = NotificationChannel(channelId,desc,NotificationManager.IMPORTANCE_HIGH)
                notificationChannel.enableLights(true)
                notificationChannel.lightColor = Color.GREEN
                notificationChannel.enableVibration(true)
                notificationManager.createNotificationChannel(notificationChannel)

                builder = Notification.Builder(this, channelId)
                    .setContentTitle("Test Notification")
                    .setContentText("Testing Notification Text!")
                    .setSmallIcon(R.drawable.ic_launcher_round)
                    .setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher))
                    .setContentIntent(pendingIntent)

            } else {
                builder = Notification.Builder(this)
                    .setContentTitle("Notification")
                    .setContentText("Test description")
                    .setSmallIcon(R.drawable.ic_launcher_round)
                    .setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher))
                    .setContentIntent(pendingIntent)
            }
            notificationManager.notify(1234, builder.build())
        }
    }
}

How do i make when i receive the notification in the notification bar and click it to open browser with custom link?

  • I dont want to use firebase for this, i hope there is a method with intents

Thanks, really appreciate it

Dani AI

Generated

your notification is tapping back into MainActivity, so nothing opens in a browser. Point the PendingIntent at an implicit ACTION_VIEW intent instead. That tells Android to hand the click to the user’s default browser (or a chooser) for whatever URL you provide. See the platform’s "Common intents" guidance for opening web pages. (developer.android.com)

Example drop-in snippet (replace your current pendingIntent and wire it into your builder). Note the runtime-safe flags and auto-cancel:

val url = "https://example.com/landing?ref=notif"
val openWeb = Intent(Intent.ACTION_VIEW, Uri.parse(url))

val pendingOpen = PendingIntent.getActivity(
    this,
    0,
    openWeb,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

builder.setContentIntent(pendingOpen).setAutoCancel(true)

A couple of gotchas that often trip folks up:

  • PendingIntent flags on Android 12+ (API 31+): you must declare mutability. Use FLAG_IMMUTABLE for simple open-URL clicks like this; reserve FLAG_MUTABLE only when you truly need to modify the intent later. (developer.android.com)
  • If you are testing on Android 13+ (API 33+), you must request the POST_NOTIFICATIONS runtime permission before your app can show notifications at all; otherwise, your code runs but nothing appears. (developer.android.com)
  • Launch directly from the notification tap; do not bounce through a receiver/service (a "notification trampoline"), which is blocked on Android 12+. Your PendingIntent.getActivity(...) approach is the correct pattern. (developer.android.com)

yes, this works with a local notification. and are right that there is no bug in the builder; the change is just the intent you attach and a couple of modern Android requirements. Also consider Intent.createChooser(openWeb, "Open with") if you want to prompt for a browser explicitly.

Recommended Answers

All 3 Replies

Hi Stefan, welcome back !

What is your status now? Have you received push notification ?

Did you check your code? is it correct? Because I don't see any bugs.

Should work fine for me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.