简体   繁体   English

使用 ACTION_VIEW 打开浏览器链接不适用于应用程序外的通知

[英]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);

It's inside onReceive from a BroadcastReceiver , to trigger the browser from a notification action.它位于来自BroadcastReceiveronReceive中,用于从通知操作中触发浏览器。 For some reason (android update maybe) it now only works when I read the notification with my app in the foreground.由于某种原因(可能是android更新),它现在仅在我在前台使用我的应用程序阅读通知时才有效。 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?关于可能发生的事情以及我应该检查什么的任何想法?

You need to use pending intent instead of simple intent.您需要使用pending intent而不是简单的意图。 because notification only triggers pending intents usually while your app is in the background.因为通知通常只会在您的应用程序处于后台时触发pending intents 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
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM