npm i --save @juit/nativescript-local-notifications
- 版本:1.2.0
- GitHub:
- NPM: https://npmjs.net.cn/package/%40juit%2Fnativescript-local-notifications
- 下载量
- 昨日: 0
- 上周: 0
- 上月: 0
为 NativeScript 7 提供简洁的本地通知
此包实现了一个用于在 iOS 和 Android 上安排和取消本地通知的最简辅助程序。
用法
本地通知插件可以通过简单地调用其构造函数轻松实例化
import { LocalNotifications } from '@juit/nativescript-local-notifications'
const localNotifications = new LocalNotifications({
androidIcon: 'icon_notification',
androidColor: 0x0FF0000,
})
安排通知
可以使用 schedule(...)
方法,配合 LocalNotificationRequest
和 Date
或显示通知前等待的秒数来安排一个本地通知
const notificationId = await localNotifications.schedule({
title: 'The title',
message: 'The message for the notification...',
data: {
// Optional, this is a payload that will be
// returned when the notification is opened
}
}, 10) // schedule in 10 seconds...
schedule(...)
方法将返回一个 Promise
,该 Promise 将解析为已安排通知的 string
id。
取消已安排的通知
可以通过使用 cancel(...)
方法取消已安排的通知(因此将不再向用户显示)
localNotifications.cancel(notificationId)
订阅通知
当通知被用户打开时,notification
事件将通过 LocalNotifications
实例发出。通常的 on(...)
、once(...)
和 off(...)
方法可用于管理订阅
localNotifications.on('notification', (notification) => {
console.log('Notification opened', notification)
})
// Notification opened: {
// id: '0ecfd51b-6e83-46a9-8b6d-61632bf91db3',
// title: 'The title',
// message: 'The message for the notification...',
// data: { ... }
// }