@hibira-io / push-web
Web推送通知 Hibira推。是网络 兼容beam的推送API的客户端:麻省理工学院许可的push的分支 beam web SDK与主机是可配置的,所以为该SDK编写的代码 更改导入后继续工作。
适用于标准的Web Push浏览器:Chrome, Edge, Firefox, Opera和 Safari 16.4+ (beam SDK没有启用Safari)。
安装
npm install @hibira-io/push-web
或加载脚本,该脚本定义全局的 HibiraPush:
<script src="https://cdn.jsdelivr.net/npm/@hibira-io/push-web@1/dist/push-web.min.js"></script>
服务工
服务于 /service-worker.js 在你的站点根目录下加载我们的:
importScripts('https://cdn.jsdelivr.net/npm/@hibira-io/push-web@1/dist/service-worker.js');
// Only if your app is not on the default cluster:
// self.HibiraPush.endpoint = 'https://ws-tyo.hibira.io';
你也可以复制 node_modules/@hibira-io/push-web/dist/service-worker.js
进入你的网站。要自己处理通知,请设置
self.HibiraPush.onNotificationReceived (与比姆斯的论点相同)
onNotificationReceived)。
使用
import { Client, TokenProvider } from '@hibira-io/push-web';
const push = new Client({
instanceId: 'YOUR_INSTANCE_ID', // portal: app page → Push notifications
// endpoint: 'https://ws.hibira.io', // the default
});
await push.start();
await push.addDeviceInterest('news');
// Authenticated users: your backend returns a user token
// (generateToken in the server SDKs).
await push.setUserId('alice', new TokenProvider({ url: '/push/user-token' }));
endpoint 是门户中应用页面上显示的推送API基础URL。
endpointOverride 被接受为同义词。
要求访客打开通知
浏览器只会让网站显示几次权限框,而访问者不会
谁点了“阻止”就不能再问了。 promptForNotifications 展示了我们的
首先打开自己的横幅(就像Gmail显示的那样),然后只打开浏览器的框
在访问者点击“打开”之后。一旦他们允许,浏览器就可以
注册,并设置您通过的兴趣和用户ID。
import { promptForNotifications } from '@hibira-io/push-web';
const { outcome } = await promptForNotifications({
instanceId: 'YOUR_INSTANCE_ID',
interests: ['orders'],
siteName: 'Acme Shop',
});
// outcome: 'granted' | 'denied' | 'dismissed' | 'skipped' | 'unsupported' | 'error'
在每个页面加载时调用它:它不会显示已经允许的访问者 通知(它只是确保它们已注册),到没有的浏览器 网络推送,或者在“现在不行”冷却下来的时候。
- “不是现在” 等待
cooldownDays(7)前再问一遍,后停止maxAsks(3).resetNotificationPrompt(instanceId)忘记答案;force: true无论如何,在你的浏览器中设置“打开通知”按钮 设置。 - 阻塞:
showBlockedHelp: true控件中的通知取消阻止 浏览器而不是什么都没有。 - 语言:英语、繁體中文,简体中文,日本語,한국어,ไทย和Tiếng Việt,
从浏览器中选择。集
locale,或将任何字符串替换为text({site}充满了siteName)。 - 看:
layout: 'bar'(默认情况下,沿着顶部或底部边缘)或'card'(角),position,theme(auto/light/dark),accent,icon,zIndex,delay。横幅位于一个阴影根,所以你的CSS没有 影响它,它的CSS不会泄漏到您的页面。 client,userId与tokenProvider,onShow和onResult也 接受;看到index.d.ts对于每个选项。
使用CDN脚本 HibiraPush.promptForNotifications({...})。要么
方式, /service-worker.js 以上仍然是需要的。
从@hikibi/push-web
Hikibi现在是Hibira,这个包裹从 @hikibi/push-web 来
@hibira-io/push-web。API是相同的:更改导入和
importScripts URL,并使用 HibiraPush 你用过的地方 HikibiPush (
旧的名字从1.2.0开始消失了)。 @hikibi/push-web 1.0.0停留在NPM上
(已弃用)并且不会获得新版本。
从beam web SDK开始
| beam web SDK | @hibira-io / push-web |
|---|---|
import * as PusherPushNotifications from '@pusher/push-notifications-web' | import * as HibiraPush from '@hibira-io/push-web' |
new PusherPushNotifications.Client({ instanceId }) | new HibiraPush.Client({ instanceId }) |
importScripts('https://js.pusher.com/beams/service-worker.js') | importScripts('https://cdn.jsdelivr.net/npm/@hibira-io/push-web@1/dist/service-worker.js') |
PusherPushNotifications.onNotificationReceived = … 在服务工作者中 | HibiraPush.onNotificationReceived = … |
其他的(start、兴趣、 setUserId, stop, clearAllState,
getRegistrationState 它的值是不变的。浏览器
在beam的第一个寄存器上再次注册 start().
许可证
麻省理工学院。基于push beam web SDK,版权所有(c) 2019 push;看到 许可证。