- 版本:1.0.3
- GitHub: https://github.com/Obsessive/nativescript-custom-local-notifications
- NPM: https://npmjs.net.cn/package/nativescript-custom-local-notifications
- 下载
- 昨天: 0
- 上周: 0
- 上个月: 0
为 Android 提供自定义声音的 NativeScript 本地通知插件
本地通知插件允许您的应用程序在不运行时显示通知。就像远程推送通知一样,但设置起来要容易得多。
对于自定义声音,在 /app/App_Resources/Android 中添加一个名为 'raw' 的文件夹,并添加您的自定义声音。
注意
此存储库是 Eddy Verbruggen(eddyverbruggen)的本地通知插件的分支。所以,非常感谢 Eddy Verbruggen - Obsessive 团队。
安装
从命令提示符转到您的应用程序根目录并执行
tns plugin add nativescript-custom-local-notifications
schedule
在 iOS 上,您需要请求权限以安排通知。您可以让 schedule
函数自动为您执行此操作(如果用户授予权限,则将安排通知),或者您可以手动调用 requestPermission
,如果您喜欢这样。
您可以向此函数传递多个选项,全部都是可选的
选项 | 描述 |
---|---|
id |
一个数字,您可以用它轻松地区分通知。默认 0。 |
title |
显示在状态栏中的标题。默认为空。 |
body |
标题下的文本。默认为空。 |
ticker |
在 Android 上,您可以在状态栏中显示不同的文本,而不是使用 body 。默认未设置,因此使用 body 。 |
at |
一个 JavaScript 日期对象,指示应显示通知的时间。默认 'now'。 |
badge |
在 iOS(以及某些 Android 设备)上,您在应用程序图标上方看到数字。在大多数 Android 设备上,您将在通知中心看到此数字。默认未设置(0)。 |
sound |
目前这仅在 Android 上使用,您可以将其设置为 null 来抑制声音。默认声音是位于 /Appresources/raw/notify.mp3 的声音文件。 |
LocalNotifications.schedule([{
id: 1,
title: 'The first title',
body: 'The first body',
ticker: 'The ticker',
badge: 1,
sound: "sound1", //sound1 from /Appresources/raw/ folder
at: new Date(new Date().getTime() + (20 * 1000))
}]).then(
function() {
console.log("Notification scheduled 1");
},
function(error) {
console.log("scheduling error: " + error);
}
);
LocalNotifications.schedule([{
id: 1,
title: 'The title',
body: 'The body',
ticker: 'The ticker',
badge: 1,
sound: null, // suppress sound on Android
at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
}]).then(
function() {
console.log("Notification scheduled");
},
function(error) {
console.log("scheduling error: " + error);
}
)
addOnMessageReceivedCallback
在通知中心轻按通知将启动您的应用程序。但如果您安排了两个通知并且想知道用户按的是哪个呢?
使用此函数在通知用于启动您的应用程序时调用回调。请注意,在 iOS 上,即使在您的应用程序在前台并且收到通知时,它也会被触发。
LocalNotifications.addOnMessageReceivedCallback(
function (notification) {
console.log("ID: " + notification.id);
console.log("Title: " + notification.title);
console.log("Body: " + notification.body);
}
).then(
function() {
console.log("Listener added");
}
)
getScheduledIds
如果您想了解所有已安排通知的 ID,请这样做
请注意,所有函数都有错误处理程序(请参阅 schedule
),但我们不会重复说明,以保持可读性。
LocalNotifications.getScheduledIds().then(
function(ids) {
console.log("ID's: " + ids);
}
)
cancel
如果您想取消先前安排的通知(并且知道其 ID),您可以取消它
LocalNotifications.cancel(5 /* the ID */).then(
function(foundAndCanceled) {
if (foundAndCanceled) {
console.log("OK, it's gone!");
} else {
console.log("No ID 5 was scheduled");
}
}
)
cancelAll
如果您只想取消先前安排的所有通知,请这样做
LocalNotifications.cancelAll();
requestPermission
在Android上不需要权限,但在iOS上则需要。Android会简单地返回true。
如果之前运行了requestPermission
或schedule
函数,用户已经被提示授予权限。如果用户授予权限,此函数返回true
,但如果他拒绝权限,此函数将返回false
,因为iOS只能请求一次权限。在这种情况下,用户需要进入iOS设置应用,手动为您的应用启用权限。
LocalNotifications.requestPermission().then(
function(granted) {
console.log("Permission granted? " + granted);
}
)
hasPermission
在Android上不需要权限,但在iOS上则需要。Android会简单地返回true。
如果之前运行了requestPermission
或schedule
函数,您可能想检查用户是否已授予权限。
LocalNotifications.hasPermission().then(
function(granted) {
console.log("Permission granted? " + granted);
}
)