cordova 每天在特定时间发送Ionic 3本地通知

6tqwzwtp  于 2022-11-15  发布在  Ionic
关注(0)|答案(3)|浏览(206)

我已经添加了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"}
  }
]);

对于当天的应用程序启动,它工作正常,但我想发送通知每天在指定的时间。
我特别想要本地通知,而不是推送通知。

svgewumm

svgewumm1#

为了使每日重复通知,你需要使用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"}
  }
]);
ikfrs5lh

ikfrs5lh2#

在他们的代码库中显示(注解),您可以通过这样做来实现这一点

this.localNotifications.schedule({
 text: 'Delayed ILocalNotification',
 trigger: {at: new Date(new Date().getTime() + 3600)},
 led: 'FF0000',
 sound: null});

现在,如果您必须每天在同一时间发送通知,您可以:

**1 -**计划十分之一的通知,并在每次用户打开您的应用时进行检查
**2 -**每次用户打开已收到的通知时重新安排通知。

628mspwn

628mspwn3#

为了生成每日重复的通知,您需要使用every:"day"firstAt属性,其中包含第一次触发通知的日期。

注意:与Ionic 3中的cordova插件不同,firstAt属性需要 Package 在trigger属性中。您可以在Ionic本地通知文档中找到更多信息。

尝试此代码

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"}
  }
]);

相关问题