Leandro 86
Leandro 86

Reputation: 167

Opening browser link with ACTION_VIEW not working for notification outside app

I had this piece of code that was working nicely not long ago:

Intent browserAction = new Intent(Intent.ACTION_VIEW, uri);
browserAction.setData(uri);
browserAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(browserAction);

It's inside onReceive of a BroadcastReceiver, to trigger the browser from a notification action (a PendingIntent that also do other things). For some reason (android update maybe) it now only works when I read the notification with my app in the foreground. If I'm outside my app and click the notification action, the browser isn't being called.

Any ideas of what may be happening and what I should check?

EDIT: If I do a PendingIntent directly from Intent.ACTION_VIEW (instead of using Intent.ACTION_VIEW inside a BroadcastReceiver) the action is fired nicely even outside the app. But I can't rely on this since my BroadcastReceiver did other things after calling the browser.

Upvotes: 0

Views: 1233

Answers (2)

Ali Rasouli
Ali Rasouli

Reputation: 1911

Follow this code:

        var notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager
        var newNotifId = (1000000..2000000).random()// or you can change it to your uniquee id

        var builder:Notification.Builder
        var description="this is my notif description!"
        var title="my custom title"
        // checking if android version is greater than oreo(API 26) or not
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "thisismyapp"
            builder  = Notification.Builder(_context, channelId)
                .setContentTitle(title)
                .setContentText(description)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(_context.resources,
                    R.drawable.ic_launcher_background
                ))
        } else {

            builder = Notification.Builder(_context)
                .setContentTitle(title)
                .setContentText(desc)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(_context.resources, R.mipmap.ic_launcher))
        }
        val notifLink="https://aliras.ir"//or your custom link
        val myuri = Uri.parse(notifLink)
        val browserAction = Intent(Intent.ACTION_VIEW,myuri )
        browserAction
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
        browserAction.setData(myuri);
        browserAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        val pendingIntent =
            PendingIntent.getActivity(
                _context,
                newNotifId,
                browserAction,
                PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE
            )
        builder.setContentIntent(pendingIntent);
        _notificationManager.notify(newNotifId, builder.build())

Upvotes: 0

Pratik PSB
Pratik PSB

Reputation: 184

You need to use pending intent instead of simple intent. because notification only triggers pending intents usually while your app is in the background. so, please try this code.

Intent browserAction = new Intent(Intent.ACTION_VIEW, uri);
browserAction.setData(uri);
browserAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Create the PendingIntent
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
        this, 0, browserAction, PendingIntent.FLAG_UPDATE_CURRENT
);

UPDATE

this answer will be useful for you. please refer to this.

refer this

Upvotes: 1

Related Questions