使用Event Notifications创建并向 Safari 网页发送推送通知
创建 Event Notifications 服务,为 Apple 推送通知服务 (Safari) 添加推送目标,并将消息发送到 iOS 设备。
什么是 Event Notifications?
Event Notifications是一项事件通知路由服务,可通知您 "IBM Cloud账户中发生的重要事件,或使用网络钩子触发自动操作。 您可以将事件通知从 IBM Cloud 服务 (例如 Availability Monitoring) 过滤和路由到电子邮件,SMS,推送通知 (FCM 或 APN) 和 Webhook。
客户如何使用 Safari 推送通知?
下图显示客户机如何使用 iOS 推送通知。
目标
本教程向您展示了如何发送推送通知,如下所示:
- 使用 Event Notifications创建 Web 应用程序。
- 获取 Safari 凭证。
- 下载代码并完成通知设置。
- 配置 Safari 推送通知并将其发送到 Safari Web。
准备工作
您必须具备以下先决条件:
- IBM Cloud 帐户。 如果您没有帐户,请 创建 IBM Cloud 帐户。
- Event Notifications 实例。
- IAM API 密钥,用于允许 SDK 访问您的帐户。 在 此处创建一个。
创建Event Notifications服务实例
- 登录到 IBM Cloud 帐户。
- 在 IBM Cloud 目录 中,搜索
Event Notifications > Event Notifications
。 - 从受支持区域列表中选择
Region
,然后选择pricing plan
。 - 提供 "
Service name
。 - 选择 "
resource group
。 - 通过单击复选框来接受许可协议和条款。
- 单击
Create
。
获取 Safari 凭证
-
将 Apple Worldwide 中间证书添加到密钥链,您可以在 此处找到证书。
Certificates, Identifiers & Profiles Certificates, Identifiers & Profiles -
在 Apple Developer 帐户中创建 Web 推送标识。
网络推送 ID(捆绑标识符)是一个唯一标识符,可识别特定应用程序。 每个应用程序都需要一个 Web 推送标识。
-
转至 Apple Developer Portal,然后选择
Certificates, Identifiers & Profiles
。{: caption="证书、标识符和" caption-side="bottom"}
-
转至
Identifiers > Add identifier button
。{: caption="标识符按钮标识符" caption-side="bottom"}
-
单击
Add a new Identifier
按钮。新标识符 -
选择“Web 推送标识”选项。
*网络推送 IDs -
提供 Web 推送标识的标识和描述,标识是 Web 推送标识的唯一反向域字符串,例如 web.com.example.domain (该字符串必须以 web 开头)。 如果您的 Web 站点是 yourwebsite.com,请将标识添加为 web.com.yourwebsite。
网络推送 -
继续,然后单击下一页上的
Create certificate
。创建证书 创建证书 -
使用 KeyChain 创建证书签名请求并将其保存到磁盘。 有关更多信息,请参阅 此处。
证书签名*证书 证书签名*证书 -
上传 CertificateSigningRequest.certSigningRequest 并单击继续。
{: caption="上传证书" caption-side="bottom"}
{: caption="证书" caption-side="bottom"}上传
-
下载具有
.cer
扩展名的证书,然后双击该证书。 它将其添加到 密钥链访问 → 我的证书。 -
获取到您的 密钥链访问 → 我的证书,选择您的证书导出该证书。
证书 {: caption="上传证书" caption-side="bottom"}
-
添加密码 (请勿忘记) 并另存为 p12 证书。
添加通用 API 源
请执行以下步骤:
- 转至 Event Notifications 仪表板的
Sources
部分。 - 单击
Add
并选择 API 源。 - 输入名称和可选描述,然后单击
Add
。
创建Event Notifications目的地
在 Event Notifications 控制台中单击 Destinations
,然后添加以下目标详细信息:
-
Name
: 添加目标的名称。 -
Description
: 添加目标的可选描述。 -
Type
: 从下拉列表中选择“Safari 推送通知”类型。 -
选择目标计划: 生产前目标或生产目标。
Pre-production destination
-针对开发和测试环境,选择此目标作为低成本推送目标。Production destination
-使用此目标的完整功能。 允许无限设备和外发信息。
-
使用详细信息更新 Safari 推送凭证。
Website Name
: Web 站点名称。 这是“通知中心”中使用的标题。Website push ID
:网站推送 ID 的唯一反向域名字符串,如web.com.example.domain(字符串必须以 web 开头)。Website URL
: The URL of the website that should be permitted to subscribe to Safari Push Notifications.URL format string
:点击通知时要转到的 URL。 使用 %@ 作为参数占位符,在发送通知时填写这些参数。 该 URL 必须使用 http 或 https 方案:否则无效。- 上载 p12 证书并提供证书
password
。
创建 Event Notifications 主题
在 Event Notifications 控制台中选择 Topics
,然后单击 Create
。 输入以下主题详细信息:
Name
: 输入主题的名称。Description
: 添加主题的可选描述。Source
:从下拉列表中选择来源。Event type
: 从下拉列表中选择事件类型。Event sub type
从事件子类型下拉列表中选择事件子类型。Severity
: 从严重性下拉列表中选择严重性。Advanced conditions
: 编写您自己的定制条件,必须遵循 jsonpath 规范。
创建Event Notifications订阅
在 Event Notifications 控制台中单击 Subscriptions
。 输入以下订购详细信息:
-
Click
创建以显示预订向导。 -
填写以下订购信息:
Subscription name
: 预订的名称。Subscription description
: 添加可选描述。
-
在
Subscribe to a topic
部分下,从下拉列表中选择主题,然后从目标下拉列表中选择目标。 -
Destination type
: 在Destination
下选择类型,然后单击Add
。
设置 Event Notifications Safari Web SDK
Safari Web SDK 使 Safari Web 站点能够接收推送通知。 完成以下步骤以安装 Event Notifications Firefox Web SDK,初始化 SDK 并注册 Web 站点的通知。
-
要在项目中包含 SDK,请将
ENPushSDK.js
,ENPushServiceWorker.js
和manifest_Website.json
文件添加到项目根文件夹。 -
编辑
manifest_Website.json
文件。{ "name": "YOUR_WEBSITE_NAME" }
-
将
manifest_Website.json
文件名更改为manifest.json
。 -
将
manifest.json
包含在 HTML 文件的<head>
标记中。<link rel="manifest" href="https://github.com/IBM/event-notifications-destination-webpush-sdk/blob/main/manifest_Website.json">
-
将 IBM Cloud Web 推送 SDK 包含到脚本中。
<script src="https://github.com/IBM/event-notifications-destination-webpush-sdk/blob/main/ENPushSDK.js" async></script>
-
完成以下步骤以启用 Web 站点来初始化 SDK
var enPush = new ENPush() function callback(response) { alert(response.response) } var initParams = { "instanceGUID": "<instance_guid>", "apikey": "<instance_apikey>", "region": "<region>", "deviceId": "<YOUR_DEVICE_ID>", "safariDestinationId": "<safari_destination_id>", "websitePushIdSafari": "<Safari Web Push Id Identifier>" } enPush.initialize(initParams, callback)
-
region
: Event Notifications 实例的区域。例如,us-south
,eu-gb
,au-syd
,eu-de
和eu-es
。 -
deviceId
: 可选deviceId
用于设备注册。
-
-
要注册通知,请使用
register()
或registerWithUserId()
API 向 IBM Cloud Event Notifications 服务注册设备。 选择以下任一选项:-
在没有
UserId
的情况下注册:enPush.register(function(response) { alert(response.response) })
-
向
UserId
注册。 对于基于userId
的通知,注册方法再接受一个参数-userId
。
bmsPush.registerWithUserId("UserId",function(response) { alert(response.response) })
UserId
是要用于在推送服务实例中注册设备的用户标识值。 -
-
subscribe
API 为标记预订设备。 设备订阅特定标签后,就能接收为该标签发送的通知。 将以下代码片段添加到 Web 应用程序以预订标记列表。enPush.subscribe(tagName, function(response) { alert(response.response) })
-
设置完成后,运行应用程序并注册推送通知。
向 Safari Web 设备发送通知
使用 发送通知 API 来发送 Firefox 设备的推送通知。 您可以使用 Node 或 Go 管理 SDK,而不是直接调用 API。
{: caption="接收通知接收" caption-side="bottom"}