Web 应用程序如何向 iOS 设备发送推送通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4939698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:50:00  来源:igfitidea点击:

How can a web application send push notifications to iOS devices?

iosiphoneweb-applicationsapple-push-notifications

提问by Frankie

I'm working on a web app. How can I send push notifications to iOS users when there is new content?

我正在开发一个网络应用程序。当有新内容时,如何向 iOS 用户发送推送通知?

采纳答案by Kris Babic

To be more specific, in order for a web application to send push notifications to a mobile device, such as the iPhone, the mobile device must have registered to receive push notifications for a particular application. The registration for push notification is done through a native app and can only be performed through a native app. Once the native app is registered for push notification, it can send the authorization token to the server, which can be used in conjunction with the certificate used to provision the native client, to send the push notifications to the mobile device.

更具体地说,为了让 Web 应用程序向移动设备(例如 iPhone)发送推送通知,移动设备必须已注册以接收特定应用程序的推送通知。推送通知的注册是通过原生应用完成的,并且只能通过原生应用进行。一旦本机应用程序注册了推送通知,它就可以将授权令牌发送到服务器,该令牌可以与用于配置本机客户端的证书结合使用,以将推送通知发送到移动设备。

As specified in another answer, one option is to 'wrap' your web application in a native application. Meaning that you would create a native application that basically presents a UIWebView (for iPhone dev) to the user showing your web application. While this pretty much functions in the same manner as the native browser, you would be able to add the ability to register for push notifications using the native controls.

如另一个答案中所述,一个选项是将您的 Web 应用程序“包装”在本机应用程序中。这意味着您将创建一个本机应用程序,该应用程序基本上向显示您的 Web 应用程序的用户呈现 UIWebView(用于 iPhone 开发)。虽然这几乎与本机浏览器的功能相同,但您可以添加使用本机控件注册推送通知的功能。

It would be beneficial to you to review the Apple's push notification document as it provides some pretty good information on how push messaging functions on the iPhone.

查看 Apple 的推送通知文档对您很有帮助,因为它提供了一些关于 iPhone 上的推送消息功能的非常好的信息。

See these links provided by Peter Hosey:

请参阅 Peter Hosey 提供的这些链接:

回答by Felix

No, only native iOS applications support push notifications.

不,只有原生 iOS 应用程序支持推送通知。

UPDATE:
Mac OS X 10.9 & Safari 7 websites can now also send push notifications, but this still does not apply to iOS. Read the Notification Programming Guide for Websites. Also check out WWDC 2013 Session 614.

更新:
Mac OS X 10.9 和 Safari 7 网站现在也可以发送推送通知,但这仍然不适用于 iOS。阅读网站通知编程指南。另请查看WWDC 2013 Session 614

回答by collimarco

While not yet supported on iOS(as of iOS 10), websites can send push notifications to Firefox and Chrome (Desktop/Android) with the Push API.

虽然iOS 尚不支持(从 iOS 10 开始),但网站可以使用Push API向 Firefox 和 Chrome(桌面/Android)发送推送通知。

The Push API is used in conjunction with the older Web Notificationsto display the message. The advantage is that the Push API allow the notification to be delivered even when the user is not surfing your website, because they are built upon Service Workers (scripts that are registered by the browser and can be executed in background at a later time even after your user has left your website).

推送API在与旧结合使用的Web通知显示该消息。优点是即使用户没有浏览您的网站,Push API 也允许发送通知,因为它们是建立在 Service Workers 之上的(由浏览器注册的脚本,可以稍后在后台执行,甚至在您的用户已离开您的网站)。

The process of sending a notification involves the following:

发送通知的过程涉及以下内容:

  1. a user visits your website (must be secured over HTTPS): you ask permission to display push notifications and you register a service worker (a script that will be executed when a push notification is received)
  2. if the user has granted permission, you can read the device token (endpoint) which should be sent to the server and stored
  3. now you can send notifications to the user: your server makes an HTTP POST request to the endpoint (which is an URL that contains the device token). The server which receives the request is owned by the browser manufacturer (e.g. Google, Mozilla): the browser is constantly connected to it and can read the incoming notifications.
  4. when the user browser receives a notification executes the service worker, which is responsible for managing the event, retrieving the notification data from the server and displaying the notification to the user
  1. 用户访问您的网站(必须通过 HTTPS 保护):您请求显示推送通知的权限并注册服务工作者(收到推送通知时将执行的脚本)
  2. 如果用户已授予权限,则可以读取应发送到服务器并存储的设备令牌(端点)
  3. 现在您可以向用户发送通知:您的服务器向端点(这是一个包含设备令牌的 URL)发出 HTTP POST 请求。接收请求的服务器归浏览器制造商所有(例如 Google、Mozilla):浏览器始终与其连接并可以读取传入的通知。
  4. 当用户浏览器收到通知时,执行 Service Worker,它负责管理事件,从服务器检索通知数据并将通知显示给用户

The Push API is currently supported on desktop and Androidby Chrome, Firefoxand Opera.

目前,ChromeFirefoxOpera桌面和 Android上支持 Push API 。

You can also send push notifications to Apple / Safaridesktop using APNs. The approach is similar, but with many complications (apple developer certificates, push packages, low-level TCP connection to APNs).

您还可以使用 APNs向 Apple / Safari桌面发送推送通知。方法类似,但有很多复杂性(苹果开发者证书、推送包、到 APNs 的低级 TCP 连接)。

If you want to implement the push notifications by yourself start with these tutorials:

如果您想自己实现推送通知,请从以下教程开始:

If you are looking for a drop in solution I would suggest Pushpad, which is a service I have built.

如果您正在寻找解决方案的下降,我会建议Pushpad,这是我建立的一项服务。

Update(September 2017): Apple has started developing the service workers for WebKit (status). Since the service workers are a fundamental technology for web push, this is a big step forward.

更新(2017 年 9 月):Apple 已开始为 WebKit ( status)开发服务工作线程。由于 Service Worker 是 Web 推送的基础技术,因此这是向前迈出的一大步。

回答by BonzaiThePenguin

Google Chrome now supports the W3C standard for push notifications.

Google Chrome 现在支持 W3C 标准的推送通知。

http://www.w3.org/TR/push-api/

http://www.w3.org/TR/push-api/

回答by Sam

You can use pushover if you don't want to create your own native app: https://pushover.net/

如果您不想创建自己的本机应用程序,可以使用 pushover:https: //pushover.net/

回答by Jonathan Nemeth

ACTUALLY.. This is brand new mind you.. On the newest version of OS X (Mavericks) you CANsend push notifications from a webpage to the desktop. But according to the documentation, NOT iPhones:

ACTUALLY。这是全新的主意你..在OS X(小牛)的最新版本,CAN从网页到桌面发送推送通知。但根据文档,不是 iPhone:

Note: This document pertains to OS X only. Notifications for websites do not appear on iOS.

Currently Apple has plans to allow 2 kinds of push notifications: OS X Website Push Notifications and Local Notifications.

目前,Apple 计划允许两种推送通知:OS X 网站推送通知和本地通知。

The obvious hurdle here is that this will not work on PCs, nor will it allow you to do android push notifications.

这里明显的障碍是这在 PC 上不起作用,也不允许您执行 android 推送通知。

Furthermore, you actually can with versions as old as Snow Leapord, send push notifications from a website as long as said website is open and active. The new Mavericks OS will allow push notifications even if the site isnt opened, assuming you have already given permission to said site to send push notifications.

此外,您实际上可以使用与 Snow Leapord 一样古老的版本,从该网站发送推送通知,只要该网站处于打开状态并处于活动状态。即使站点未打开,新的 Mavericks 操作系统也将允许推送通知,假设您已经授予该站点发送推送通知的权限。

From the mouth of Apple:

出自苹果之口:

In OS X v10.9 and later, you can dispatch OS X Website Push Notifications from your web server directly to OS X users by using the Apple Push Notification service (APNs). Not to be confused with local notifications, push notifications can reach your users regardless of whether your website or their web browser is open…

在 OS X v10.9 及更高版本中,您可以使用 Apple 推送通知服务 (APN) 从 Web 服务器直接向 OS X 用户发送 OS X 网站推送通知。不要与本地通知混淆,无论您的网站或他们的网络浏览器是否打开,推送通知都可以到达您的用户......

To integrate push notifications in your website, you first present an interface that allows the user to opt in to receive notifications. If the user consents, Safari contacts your website requesting its credentials in the form of a file called a push package. The push package also contains notification assets used throughout OS X and data used to communicate to a web service you configure. If the push package is valid, you receive a unique identifier for the user on the device known as a device token. The user receives the notification when you send the combination of this device token and your message, or payload, to APNs.

要在您的网站中集成推送通知,您首先要展示一个界面,允许用户选择接收通知。如果用户同意,Safari 会联系您的网站,以称为推送包的文件形式请求其凭据。推送包还包含在整个 OS X 中使用的通知资产和用于与您配置的 Web 服务通信的数据。如果推送包有效,您会收到设备上用户的唯一标识符,称为设备令牌。当您将此设备令牌和您的消息或负载的组合发送到 APN 时,用户会收到通知。

Upon receiving the notification, the user can click on it to open a webpage of your choosing in the user's default browser.

收到通知后,用户可以单击它以在用户的​​默认浏览器中打开您选择的网页。

Note: If you need a refresher on APNs, read the “Apple Push Notification Service” chapter in Local and Push Notification Programming Guide. Although the document is specific to iOS and OS X push notifications, paradigms of the push notification service still apply.

注意:如果您需要复习 APNs,请阅读本地和推送通知编程指南中的“Apple 推送通知服务”一章。尽管该文档特定于 iOS 和 OS X 推送通知,但推送通知服务的范例仍然适用。

回答by JustSid

No, there is no way for an webapp to receive push notification. What you could do is to wrap your webapp into a native app which has push notifications.

不,网络应用程序无法接收推送通知。您可以做的是将您的 web 应用程序包装到具有推送通知的本机应用程序中。

回答by Foyzul Karim

You can use HTML5 Websockets to introduce your own push messages. From Wikipedia:

您可以使用 HTML5 Websockets 来引入您自己的推送消息。来自维基百科

"For the client side, WebSocket was to be implemented in Firefox 4, Google Chrome 4, Opera 11, and Safari 5, as well as the mobile version of Safari in iOS 4.2. Also the BlackBerry Browser in OS7 supports WebSockets."

“对于客户端,WebSocket 将在 Firefox 4、Google Chrome 4、Opera 11 和 Safari 5 以及 iOS 4.2 中的 Safari 移动版本中实现。OS7 中的黑莓浏览器也支持 WebSockets。”

To do this, you need your own provider server to push the messages to the clients.
If you want to use APN (Apple Push Notification) or C2DM (Cloud to Device Message), you must have a native application which must be downloaded through the online store.

为此,您需要自己的提供程序服务器将消息推送到客户端。
如果您想使用 APN(Apple 推送通知)或 C2DM(云到设备消息),您必须有一个必须通过在线商店下载的本机应用程序。

回答by mgutt

The W3C started in 2010 a working group to implement notifications:
http://www.w3.org/2010/web-notifications/

W3C 于 2010 年成立了一个工作组来实施通知:http:
//www.w3.org/2010/web-notifications/

This Working Group develops APIs that expose those mechanisms to Web Applications—so that Web developers creating, for example, Web-based e-mail clients and instant-messaging clients can more closely integrate their Web application behavior with the notification features of the operating systems of their end users.

该工作组开发的 API 将这些机制公开给 Web 应用程序,以便 Web 开发人员创建例如基于 Web 的电子邮件客户端和即时消息客户端可以更紧密地将其 Web 应用程序行为与操作系统的通知功能集成他们的最终用户。

Finally the result is like a bad joke as it works only if the specific website is open: http://alxgbsn.co.uk/notify.js/

最后的结果就像一个坏笑话,因为它只有在特定网站打开时才有效:http: //alxgbsn.co.uk/notify.js/

I think they missed to implement the possibility to add push urls so the browser is able to ask for notifications while its running in the background - and above all - if all tabs have been closed.

我认为他们错过了添加推送 url 的可能性,以便浏览器能够在后台运行时请求通知 - 最重要的是 - 如果所有选项卡都已关闭。

回答by Tim

Pushbulletis a great alternative for this.

Pushbullet是一个很好的选择。

However the user needs to have a Pushbullet account and the app installed (iOS, Android) or plugin installed (Chrome, Opera, Firefox and Windows).

但是,用户需要有一个 Pushbullet 帐户并安装应用程序(iOS、Android)或安装插件(Chrome、Opera、Firefox 和 Windows)。

You can use the APIby creating a Pushbullet app, and connect your application's user to the Pushbullet user using oAuth2.

您可以通过创建 Pushbullet 应用程序来使用API,并使用 oAuth2 将应用程序的用户连接到 Pushbullet 用户。

Using a library would make it much easier, for PHP I could recommend ivkos/Pushbullet-for-PHP.

使用库会更容易,对于 PHP,我可以推荐ivkos/Pushbullet-for-PHP