javaandroidandroid-intentandroid-notifications

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.


Solution

  • 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