UNNotification
UNNotification 表示一条已经送达的通知。它只能由系统给:从 getDeliveredNotifications()、notification 事件和 notificationclick 事件的 response.notification 来。
它是只读的值:字段不能改,写了抛 TypeError。它是普通的 JS 值,不继承 NSObject。
要先装 @nacrejs/user-notifications,装法见 UNUserNotificationCenter,也可以看核心和能力包。
继承关系:UNNotification
构造函数
不能用 new 创建。new UNNotification() 抛 TypeError:
Illegal constructor: UNNotification comes from the system (delivered notifications and the notification events)实例属性
| 属性 | 说明 |
|---|---|
date 只读 | 送达的时间 |
request 只读 | 它来自哪个请求 |
示例
js
const center = UNUserNotificationCenter.currentNotificationCenter();
center.addEventListener('notification', (event) => {
const notification = event.notification;
console.log(notification.request.identifier, notification.date.toISOString());
});效果(未在模拟器验证):App 在前台时到了一条通知,输出它的标识和送达时间。
对应的 iOS API
| ObjC | JS |
|---|---|
notification.date(NSDate) | notification.date(Date) |
notification.request | notification.request |
参见
UNNotificationRequest:request的类型