如何在 UWP 应用中的 XAML Webview 中添加对 HTML5 通知的支持? [英] How to add support for HTML5 notifications in XAML Webview in a UWP app?
问题描述
所以我在我的 UWP 应用程序中使用了 WebView
并且我想处理 HTML5 通知.我所做的是为我的 webview 添加对 ScriptNotify
事件的支持(此处).
So I am using a WebView
in my UWP application and I would like to handle HTML5 notifications. What I did was to add support for the ScriptNotify
event to my webview (here).
MyWebView.ScriptNotify += WebView_Notify;
private void WebView_Notify(object sender, NotifyEventArgs e)
{
Debug.WriteLine("NOTIFIED " + e.Value);
}
然后使用 InvokeScriptAsync
我运行了以下 javascript 代码:
Then using InvokeScriptAsync
I ran the following javascript code:
(function() {
var N = window.Notification;
window.external.notify(N.permission);
var P = function (title, options){
window.external.notify(title)
};
P.permission = 'granted';
N = P
})();
然而,这不起作用.在我的调试输出中,我确实得到:
However, this does not work. In my debug output I do get:
"通知默认`
这意味着正在触发 ScriptNotify
处理程序.但是,如何在我的应用中实现 HTML5 通知支持?
which means that ScriptNotify
handler is being triggered. However, how can implement HTML5 Notifications support in my app?
推荐答案
我找到了一个可能对您有所帮助的新解决方案.
I found a new solution that might help you.
页面加载时发起通知请求,WebView可以拦截它,使用事件PermissionRequested
.
A notification request is initiated when the page loads, WebView can intercept it, using the event PermissionRequested
.
private void WebView_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
if (args.PermissionRequest.PermissionType == WebViewPermissionType.WebNotifications)
{
args.PermissionRequest.Allow();
}
}
您也可以在监听请求时弹出一个窗口,让用户选择是否接受通知.
You can also pop up a window when you listen for a request, letting the user choose whether to accept the notification.
最好的问候.
这篇关于如何在 UWP 应用中的 XAML Webview 中添加对 HTML5 通知的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!