跳转到内容

通知

FRVR.notifications 在同一个命名空间下处理两件不同的事:

  • 预定的远程消息 —— 由服务器在未来某个时间向这个玩家投递推送(Facebook 上是聊天机器人消息)。
  • 本地通知 —— 应用在后台时,设备在 N 秒后弹一条通知。
if (await FRVR.notifications.canScheduleMessages()) {
  showReminderToggle();
}

需要用户同意 —— 很多渠道要求玩家显式 opt-in 之后才能预约:

await FRVR.notifications.subscribeScheduleMessages();
await FRVR.notifications.scheduleMessage({
  delayInSeconds: 24 * 3600,          // 1 天
  imageUrl:       '/push/daily.png',
  message:        "Don't forget your daily reward!",
  playerId:       FRVR.profile.id(),
  buttons: [
    { type: 'postback', title: 'Play now', payload: JSON.stringify({ deepLink: 'daily' }) },
  ],
});

当玩家通过这条通知进入游戏时,buttons[].payload 会出现在 入口 数据里。

适合短时提醒,例如「30 分钟后你的体力满了」:

FRVR.notifications.scheduleLocalNotification(
  'energy_full',                       // code(用于取消 / 替换)
  'Energy full!',                      // 标题
  'Come back and play a few rounds',   // 正文
  30 * 60,                             // 秒
);

用同一个 code 再次安排,会替换之前那条通知。

不是每个渠道都有本地通知。supportsLocalNotifications() 同步返回,且不会向玩家弹任何提示,所以可以在加载通知相关资源之前先检查:

if (FRVR.notifications.supportsLocalNotifications()) {
  setUpReminders();
}

它不反映权限。通知最终会不会显示,仍由 canScheduleMessages() 决定。

await FRVR.notifications.unscheduleLocalNotification('energy_full');   // 按 code 取消一条
await FRVR.notifications.cancelAllLocalNotifications();                // 取消所有待发的

两个方法在任何地方调用都是安全的。如果这个 code 从未安排过、已经触发,或渠道没有本地通知,它们什么都不做。

方法作用
canScheduleMessages()这个玩家能否收到通知?
subscribeScheduleMessages()请求玩家订阅。玩家同意则 resolve true。
scheduleMessage(params)安排一条远程消息。
supportsLocalNotifications()渠道是否有本地通知?同步返回。
scheduleLocalNotification(code, title, text, seconds)安排一条本地通知。
unscheduleLocalNotification(code)取消一条待发的本地通知。
cancelAllLocalNotifications()取消所有待发的本地通知。

通知:可用性 + 订阅

loading SDK…