跳转到主要内容
本指南解释如何管理 Web 推送 订阅 以及订阅状态如何受到用户操作和浏览器行为的影响。

理解推送权限

用户必须给予您的网站权限才能向他们发送推送通知。如果不使用系统级权限提示明确授予站点权限,就不可能接收推送通知。

Chrome 必需的系统级权限提示示例,您必须点击'允许'才能接收此站点的推送通知。

权限可以是:
  • Default: permission has not been granted to denied.
  • Granted: you allowed the website to send you notifications.
  • Denied: you blocked the website to send you notifications. This can be a temporary block if you clicked the x to close the prompt repeatedly or a permanent block if you clicked Block or toggled off permission in the browser settings.
有关原生系统级权限提示和/或任何 OneSignal 提示的更多详情,请参阅 Web 权限提示

如何取消订阅 Web 通知

您可以通过三种方式取消订阅 Web 推送通知:

在浏览器设置中取消订阅

您可以在浏览器设置中直接管理或删除通知权限。这里是快速访问 URL 和官方文档以了解更多信息: 在这些页面上,只需点击选项删除或阻止您不希望接收通知的网站。

在网站上取消订阅

Reset permission 大多数浏览器在 URL 旁边都有一个“锁”或“设置”图标。点击它会显示特定站点权限,用户可以在此处禁用推送通知。

示例显示 Chrome 浏览器,您可以完全切换站点的推送权限或重置权限以允许站点再次提示您。

OneSignal prompts 如果网站包含 OneSignal 铃当提示自定义链接提示,用户可以直接通过这些 UI 元素取消订阅,并能够按需使用相同方式重新订阅。

示例显示 OneSignal 铃当提示。

如果您删除历史记录和/或删除您的 Cookie 和网站数据,它将临时阻止通知显示。但是,如果您不从网站中删除推送权限,您可能会在返回网站时自动重新订阅并再次开始收到通知。

示例显示清除浏览器历史记录和站点数据。


示例显示清除站点数据。


如何测试您的权限提示

这些步骤解释如何像首次访问者一样测试您的提示和订阅流程。
1

访问已设置 OneSignal SDK 的站点。

Do not use an incognito, private, or guest browser setting. This example uses Chrome version 135 on macOS but the flow should be relatively the same for most browsers.
2

重置推送权限

Click the site settings or lock icon next to the site URL and select Reset permission or remove permissions for Notifications. Skip to the next step if you don’t see this permission option.

Chrome 站点设置菜单 > 重置通知权限。

3

删除站点数据。

Click Cookies and site data > Manage on-device site data or follow the browser’s flow to see your site’s data option.

Chrome 的设备上站点数据屏幕。

删除您网站的数据并退出设置以返回您的网站。

示例显示清除您的站点 Cookie。

4

打开开发者工具。

Usually you can just right click the screen and press Inspect.
5

按照步骤提示推送通知,在所需的系统级权限提示中选择"允许"。

If you do not see the prompt or don’t know the steps, see Web permission prompts.

示例显示 Chrome 必需的系统级提示。

6

检查控制台是否有任何错误。

If you see anything in red related to OneSignal, see our Web SDK troubleshooting docs.
7

获取订阅 ID

控制台中输入或复制粘贴此代码:OneSignal.User.PushSubscription.id
  1. 这将记录您的 OneSignal 订阅 ID。将其复制粘贴到 OneSignal 仪表板的受众 > 订阅选项卡中。
  2. 如果控制台中未记录订阅 ID,则您未成功订阅。请查看 Web SDK 故障排除 了解详情。

使用控制台获取推送订阅 ID。

在 OneSignal 控制板订阅页面查看订阅 ID。

8

在订阅旁边,选择 3 个点的选项按钮并选择"添加到测试订阅"。然后命名和标记测试用户以便识别。

将您的订阅添加为测试订阅。

9

导航至消息 > 推送 > 新消息 > 新推送,在推送创建表单中添加消息。

See Push for more details if needed.

创建新的推送发送到您的测试订阅。

10

选择"测试和预览",找到并勾选您的测试订阅,然后点击"发送测试推送"。

给自己发送测试推送。

11

您应该收到测试的推送。

If you did not receive a push, see Web push: Notifications not shown for further debugging.

已接收测试推送。

You have successfully setup web push with OneSignal. Next steps:

在浏览器关闭时接收通知

浏览器在不同平台上的行为不同。请参阅下表了解在浏览器关闭时仍可接收通知的支持情况。
Browser NameAndroidWindowsmacOS
Chrome / ChromiumYesYesNo
FirefoxYesYesNo
SafariN/AN/AYes
OperaYesYesNo
EdgeYesYesNo
Chrome - Chrome runs as a background process by default even when all the windows are closed. As long as the background process is running, notifications will still be received. If the Chrome background process is not running, notifications will not be received. Firefox - On Mac OS X, the process still exists even if windows are closed, and a notification can be received if all windows are closed (as long as there is still a dot in the dock showing Firefox is still running). On Windows, the process exits after all windows are closed so notifications cannot be received unless a Firefox window is still open. Safari - Safari does not have to be running to receive notifications, as they are sent directly to the operating system. The user still has to sign up for Safari web notifications, but after that they will be received even when Safari is completely closed. 订阅者最多有 3 天时间来检索最后一个已知的缺失通知,之后消息将永久过期。 For example, suppose a subscriber was supposed to receive a Firefox web push notification, but Firefox was closed. If the subscriber opens Firefox within 3 days, the subscriber will receive only the last known web push notification that didn’t expire. If the subscriber opens Firefox after 3 days, the web push notification sent more than 3 days ago will not be received.
I