每天在特定时间发送 Ionic 3 本地通知 [英] Send Ionic 3 Local Notification every day at a specific time
问题描述
我已使用以下命令将 Ionic 3 本地通知插件添加到我的项目中:
I have added the Ionic 3 local notification plugin to my project using these commands:
ionic cordova plugin add cordova-plugin-local-notification
npm install --save @ionic-native/local-notifications
我在构造函数上添加了所有依赖项.
I added all dependencies on my constructor.
我的代码是:
let year = new Date().getFullYear();
let month = new Date().getMonth();
let day = new Date().getDate();
let time1 = new Date(year, month, day, 10, 00, 0, 0);
let time2 = new Date(year, month, day, 12, 00, 0, 0);
this.localNotifications.schedule([
{
id: 1,
title: 'My first notification',
text: 'First notification test one',
trigger: { at: new Date(time1) },
data: {"id": 1, "name": "Mr. A"}
},
{
id: 2,
title: 'My Second notification',
text: 'Second notification on 12 pm',
trigger: { at: new Date(time2) },
data: {"id": 2, "name": "Mr. B"}
}
]);
它适用于当天应用启动,但我想每天在指定时间发送通知.
It works fine for the current day app start, but I want to send a notification every day at the specified time.
我特别想要本地通知,而不是推送通知.
I want local notifications specifically, not push notifications.
推荐答案
所以我自己找到了解决方案.这是 LocalNotifications 包的类型文件中的错误.
So I found the solution to this one by myself. It's an error in the typings file of LocalNotifications package.
选项的正确用法如下所示:
The correct usage for the options look like this:
{
id: 1,
title: 'Notification Title',
text: 'Your notification text',
foreground: true,
trigger: {
every: {
hour: 8,
minute: 15
}
}
}
只需进入您的 node_modules/@ionic-native/local-notifications 找到 index.d.ts 并找到显示 every?: ELocalNotificationTriggerUnit 的行并将其更改为 every?: any;现在它应该可以正常工作了.
Just go into your node_modules/@ionic-native/local-notifications find the index.d.ts and find the line which says every?: ELocalNotificationTriggerUnit and change it to every?: any; now it should work perfectly.
这篇关于每天在特定时间发送 Ionic 3 本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!