0

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.

Leandro 86
  • 157
  • 1
  • 9

1 Answers1

0

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

Pratik PSB
  • 177
  • 1
  • 18
  • That solved partially! Actually I was already using PendingIntent for the notification, but it was with another Action that used ACTION_VIEW inside it. You gave me the idea of simplifying it. Instead of using a custom Action that invoked the ACTION_VIEW, I just used that directly in the Pending Intent, as you wrote. Now the browser opens outside the app again! But I got a new problem: one of the things I did in my custom Action was calling NotificationManagerCompat.cancelAll to close the notification after the action. Without that, the notification isn't removed when I click the action. – Leandro 86 Feb 03 '22 at 18:53
  • you have to use a notification_id to cancel the notification after perform any action – Pratik PSB Feb 04 '22 at 07:20
  • in pending_intent we pass 0 by default for notification id. you have to use your own notification id for this. to dismiss the notification after perform some action – Pratik PSB Feb 04 '22 at 07:24
  • But looking at the code you suggested, where would you cancel the notification? When I used a BroadcastReceiver I could do that. But using a PendingIntent directly with Intent.ACTION_VIEW I have no control over the action's execution. – Leandro 86 Feb 04 '22 at 17:22