使用Event Notifications创建并向Firefox网络发送推送通知
创建 Event Notifications 服务,为 Firefox添加推送目标,并将消息发送到 Firefox Web 设备。
什么是 Event Notifications?
Event Notifications是一项事件通知路由服务,可通知您 "IBM Cloud账户中发生的重要事件,或使用网络钩子触发自动操作。 您可以将事件通知从 IBM Cloud 服务 (例如 Availability Monitoring) 过滤和路由到电子邮件,SMS,推送通知和 Webhook。
客户机如何使用 Firefox Web 推送通知?
下图显示客户机如何使用 Firefox Web 推送通知。
目标
本教程向您展示了如何发送推送通知,如下所示:
- 使用 Event Notifications创建 Web 站点。
- 获取 Firefox Web 凭证。
- 下载 SDK 并完成通知设置。
- 配置 Firefox Web 推送通知并将其发送到浏览器。
创建Event Notifications服务实例
- 登录到 IBM Cloud 帐户。
- 在 IBM Cloud 目录 中,搜索
Event Notifications > Event Notifications
。 - 从受支持区域列表中选择
Region
,然后选择pricing plan
。 - 提供 "
Service name
。 - 选择 "
resource group
。 - 通过单击复选框来接受许可协议和条款。
- 单击
Create
。
添加通用 API 源
请执行以下步骤:
- 转至 Event Notifications 仪表板的
Sources
部分。 - 单击
Add
并选择 API 源。 - 输入名称和可选描述,然后单击
Add
。
创建 Event Notifications 目标
在 Event Notifications 控制台中单击 Destinations
,然后添加以下目标详细信息:
Name
: 添加目标的名称。Description
: 添加目标的可选描述。Type
: 从下拉列表中选择Firefox Push Notifications
类型。- 选择目标计划: 生产前目标或生产目标。
Pre-production destination
-针对开发和测试环境,选择此目标作为低成本推送目标。Production destination
-利用此目标的全部功能。 允许无限设备和外发信息。
- 提供您网站的 URL。
- 单击添加。
- 创建 Firefox 目标后。 编辑创建的目标以获取 Web sdk 所需的
public vapId key
。


创建 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 Firefox Web SDK
Firefox Web SDK 使 Firefox 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>", "firefoxDestinationId": "<firefox_destination_id>", "firefoxApplicationServerKey": "<Firefox_VapId_public_key>" } 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) })
-
设置完成后,运行应用程序并注册推送通知。
向 Firefox 设备发送通知
使用 发送通知 API 来发送 Firefox 设备的推送通知。 您可以使用 Node 或 Go 管理 SDK,而不是直接调用 API。
{: caption="接收通知接收" caption-side="bottom"}