如何从通知中心的应用程序中的WebView中加载的网站接收通知?

时间:2022-11-30 08:49:45

I am creating an Mac app with a WebView that loads a website that can send web notifications. I want these notifications to appear in the notification center. How would I do this?

我正在创建一个带有WebView的Mac应用程序,它可以加载可以发送Web通知的网站。我希望这些通知显示在通知中心。我该怎么做?

1 个解决方案

#1


I think what you're saying is that you don't want to send notifications yourself from your app, but you want to allow web notifications sent by a website encapsulated in your app's WebView to appear as they do with Safari. Is that right?

我想你所说的是你不想自己从你的应用程序发送通知,但你想允许封装在应用程序的WebView中的网站发送的网络通知与Safari一样。是对的吗?

If so, the Notification Programming Guide for Websites seems to answer this in the first few paragraphs. They make a distinction between push notifications and local notifications. Push notifications are sent at any time, whether you're browsing the website or not; local notifications are invoked by JavaScript from the loaded web page while you're browsing. Push notifications happen outside any app and you don't have to do anything extra; local notifications have to be set up (with JavaScript on the website), per the guide. Nothing further appears to be necessary, aside from the user allowing notifications from that site or any sites.

如果是这样,网站通知编程指南似乎在前几段回答了这个问题。它们区分了推送通知和本地通知。无论您是否浏览网站,都会随时发送推送通知;在您浏览时,JavaScript会从加载的网页调用本地通知。推送通知发生在任何应用程序之外,您不必做任何额外的事情;根据指南,必须设置本地通知(在网站上使用JavaScript)。除了允许来自该站点或任何站点的通知的用户之外,似乎没有必要进一步。

It's unclear from the guide whether you can (or even have to) handle this from a WebView in your own app, but the guide specifies the notification object is provided by WebKit (the engine behind Safari and your own app's WebView), so I guess it should work. Have you tried it already?

从指南中不清楚你是否可以(或者甚至不得不)在你自己的应用程序中从WebView处理这个,但是指南指定通知对象是由WebKit(Safari背后的引擎和你自己的应用程序的WebView)提供的,所以我猜它应该工作。你有没试过吗?

Armed with this information, I recommend reading the guide thoroughly and trying it. If you run into problems, edit your post (if you can - I notice you're a new SO user; if you can't, post it as a comment and someone can help get it into the body of your post), and include a detailed description of the steps you took to try this, as well as the relevant code (including the JavaScript from the website), and what exactly does or doesn't happen. This will help us help you.

有了这些信息,我建议您仔细阅读本指南并进行尝试。如果您遇到问题,请编辑您的帖子(如果可以 - 我注意到您是新的SO用户;如果您不能,请将其发布为评论,有人可以帮助您将其发布到帖子的正文中),以及包括您尝试此操作所采取步骤的详细说明,以及相关代码(包括网站上的JavaScript),以及究竟发生或未发生的情况。这将有助于我们为您提供帮助。

#1


I think what you're saying is that you don't want to send notifications yourself from your app, but you want to allow web notifications sent by a website encapsulated in your app's WebView to appear as they do with Safari. Is that right?

我想你所说的是你不想自己从你的应用程序发送通知,但你想允许封装在应用程序的WebView中的网站发送的网络通知与Safari一样。是对的吗?

If so, the Notification Programming Guide for Websites seems to answer this in the first few paragraphs. They make a distinction between push notifications and local notifications. Push notifications are sent at any time, whether you're browsing the website or not; local notifications are invoked by JavaScript from the loaded web page while you're browsing. Push notifications happen outside any app and you don't have to do anything extra; local notifications have to be set up (with JavaScript on the website), per the guide. Nothing further appears to be necessary, aside from the user allowing notifications from that site or any sites.

如果是这样,网站通知编程指南似乎在前几段回答了这个问题。它们区分了推送通知和本地通知。无论您是否浏览网站,都会随时发送推送通知;在您浏览时,JavaScript会从加载的网页调用本地通知。推送通知发生在任何应用程序之外,您不必做任何额外的事情;根据指南,必须设置本地通知(在网站上使用JavaScript)。除了允许来自该站点或任何站点的通知的用户之外,似乎没有必要进一步。

It's unclear from the guide whether you can (or even have to) handle this from a WebView in your own app, but the guide specifies the notification object is provided by WebKit (the engine behind Safari and your own app's WebView), so I guess it should work. Have you tried it already?

从指南中不清楚你是否可以(或者甚至不得不)在你自己的应用程序中从WebView处理这个,但是指南指定通知对象是由WebKit(Safari背后的引擎和你自己的应用程序的WebView)提供的,所以我猜它应该工作。你有没试过吗?

Armed with this information, I recommend reading the guide thoroughly and trying it. If you run into problems, edit your post (if you can - I notice you're a new SO user; if you can't, post it as a comment and someone can help get it into the body of your post), and include a detailed description of the steps you took to try this, as well as the relevant code (including the JavaScript from the website), and what exactly does or doesn't happen. This will help us help you.

有了这些信息,我建议您仔细阅读本指南并进行尝试。如果您遇到问题,请编辑您的帖子(如果可以 - 我注意到您是新的SO用户;如果您不能,请将其发布为评论,有人可以帮助您将其发布到帖子的正文中),以及包括您尝试此操作所采取步骤的详细说明,以及相关代码(包括网站上的JavaScript),以及究竟发生或未发生的情况。这将有助于我们为您提供帮助。