UNNotificationPresentEvent
UNNotificationPresentEvent 表示 App 在前台时,一条通知送达了。
UNUserNotificationCenter 的 notification 事件是它,notification 是刚到的那一条。
它属于 @nacrejs/user-notifications 这个包,装法见 UNUserNotificationCenter。
继承关系:Event → UNNotificationPresentEvent
构造函数
创建一个 UNNotificationPresentEvent。运行时派发下面这些事件时自己创建它;App 的代码可以用它建一个事件对象,用 dispatchEvent() 派发出去,试一试监听器。
实例属性
还继承 Event 的属性,最常用的是 type 和 target。下表的属性都是只读的,给它们赋值抛 TypeError。
| 属性 | 说明 |
|---|---|
notification 只读 | 刚到的那一条 |
这些属性在原型上,不在事件对象自己身上:Object.keys(event) 和 { ...event } 拿不到它们,要一个一个读。见带字段的事件。
哪些事件是这个类
| 对象 | 事件 |
|---|---|
UNUserNotificationCenter | notification |
不能取消:cancelable 是 false。这条通知在屏幕上怎么展示,派发之前就按 foregroundPresentationOptions 定了。
示例
js
const center = UNUserNotificationCenter.currentNotificationCenter();
center.addEventListener('notification', (e) => {
console.log(e instanceof UNNotificationPresentEvent, e.notification.request.identifier);
});效果:App 在前台时到了一条通知,输出 true 和通知的标识。未在模拟器验证。
对应的 iOS API
| ObjC | JS |
|---|---|
userNotificationCenter:willPresentNotification:withCompletionHandler: | notification 事件 |
参数 notification | event.notification |
completionHandler(options) | 预先设好的 foregroundPresentationOptions |
参见
UNUserNotificationCenter:派发这个事件的对象UNNotification