把用户带回来的推送通知
到货通知、提醒与回复,通过一个兼容 Beams 的 API 发送到 iOS、Android 与网页,用户自选订阅主题,并可直接打开对应页面。
你大部分的用户此刻并不在你的 App 里。时机恰当的推送通知(“你关注的商品补货了”、“你的课程 15 分钟后开始”、“Ann 回复了你”)会用他们在乎的理由把人带回来。Hibira Push 通过一个兼容 Beams 的 API 发送到 iOS、Android 与浏览器,逻辑只要写一次。
用户得到什么
- 自己选择关注的事情有通知:降价、支持的队伍比赛、新的一集。
- 个人提醒与回复,送到他们正在用的设备上。
- 点一下就打开对应的页面,而不是首页。
用到的功能
- Interest(订阅主题):设备订阅的主题,例如
price-drop-sku-123或team-tigers,一次发布就送给所有关注的人。 - 已验证的用户:发布给一个用户 ID,送到该用户所有已登录的设备。
- 一个请求同时送 APNs、FCM 与 Web Push,搭配网页、Android 与 iOS SDK。
- 沿用现有的 Beams 代码:官方 Beams 服务器 SDK 指向你的端点即可使用。
步骤 1:让用户选择要关注什么
用户点“通知我”时,让设备订阅一个 interest:
import { Client } from '@hibira-io/push-web';
const push = new Client({ instanceId: 'YOUR_INSTANCE_ID', endpoint: 'https://ws.hibira.io' });
// Browsers only show the permission prompt after a click or tap.
notifyButton.addEventListener('click', async () => {
await push.start();
await push.addDeviceInterest(`price-drop-sku-${sku}`);
notifyButton.textContent = 'We will tell you';
});Android 与 iOS 也有同样的 addDeviceInterest;各平台的设置请看推送通知指南。
步骤 2:事情发生时发布
const PushNotifications = require('@pusher/push-notifications-server');
const beams = new PushNotifications({ instanceId: 'YOUR_INSTANCE_ID', secretKey: process.env.HIBIRA_PUSH_SECRET_KEY, endpoint: 'ws.hibira.io' });
beams.protocol = 'https';
await beams.publishToInterests([`price-drop-sku-${sku}`], {
web: { notification: { title: 'Back in stock', body: `${name} is back, now ${price}.`, deep_link: `https://your-shop.example/p/${sku}` } },
fcm: { notification: { title: 'Back in stock', body: `${name} is back, now ${price}.` }, data: { url: `/p/${sku}` } },
apns: { aps: { alert: { title: 'Back in stock', body: `${name} is back, now ${price}.` } }, data: { url: `/p/${sku}` } },
});一次发布就会送到所有关注这个 interest 的设备。
步骤 3:给已登录用户的个人消息
登录后把设备链接到你的用户 ID(SDK 会调用你的 token 路由),然后发布给该用户:
await beams.publishToUsers([String(userId)], {
fcm: { notification: { title: 'Your class starts in 15 minutes', body: 'Yoga with Mei, studio 2.' } },
apns: { aps: { alert: { title: 'Your class starts in 15 minutes', body: 'Yoga with Mei, studio 2.' } } },
web: { notification: { title: 'Your class starts in 15 minutes', deep_link: 'https://your-site.example/bookings' } },
});用户退出登录时,在浏览器调用 push.stop()(Android 与 iOS 为 clearAllState());用户删除账号时,在服务器调用 beams.deleteUser(userId)。
小提醒
- 在权限真正有用的时刻才询问(“补货时告诉我”),不要在第一次浏览页面时就问;这样同意的人会多很多。
- 每件可以关注的事情用一个 interest。Interest 很便宜;用户没要求的通知,只会让你的 App 被设成静音。
- 发布之前先在服务器上依用户所在时区避开勿扰时段。
- 推送与实时功能搭配:用户在 App 里时,用实时站内通知直接在画面上显示同一件事,并略过推送。