我已经使用这些命令将 Ionic 3 本地通知插件添加到我的项目中:
ionic cordova plugin add cordova-plugin-local-notification
npm install --save @ionic-native/local-notifications
我在构造函数上添加了所有依赖项。
我的密码是:
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"}
}
]);
对于当天的应用程序启动来说它工作正常,但我想在指定的时间每天发送通知。
我特别想要本地通知,而不是推送通知。
为了每天重复通知,您需要使用
every:"day"
(或以分钟为单位的间隔:every: 24*60
)和 firstAt
属性以及第一次触发通知的日期。试试这个代码
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',
firstAt: new Date(time1),
every: 24*60,
data: {"id": 1, "name": "Mr. A"}
},
{
id: 2,
title: 'My Second notification',
text: 'Second notification on 12 pm',
firstAt: new Date(time2),
every: 24*60,
data: {"id": 2, "name": "Mr. B"}
}
]);
在他们的代码库中显示(评论)您可以通过这样做来实现
this.localNotifications.schedule({
text: 'Delayed ILocalNotification',
trigger: {at: new Date(new Date().getTime() + 3600)},
led: 'FF0000',
sound: null});
现在,如果您必须每天在同一时间发送通知,您可以:
1 - 安排十分之一的通知并在用户每次打开您的应用程序时检查
2 - 每次用户打开已收到的通知时重新安排通知。
为了每天重复通知,您需要使用
every:"day"
和 firstAt
属性以及第一次触发通知的日期。
注意:与 Ionic 3 中的 cordova 插件不同,
firstAt
属性需要包装在 trigger
属性中。您可以在Ionic Local Notification Documentation中找到更多信息。
试试这个代码
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: {firstAt: new Date(time1)},
every: every: "day"
data: {"id": 1, "name": "Mr. A"}
},
{
id: 2,
title: 'My Second notification',
text: 'Second notification on 12 pm',
trigger: {firstAt: new Date(time2)},
every: "day", //"day","hour","minute","week" can be used
data: {"id": 2, "name": "Mr. B"}
}
]);