Send Ionic 3 Local Notification every day at a specific time

可紊 提交于 2019-12-02 05:13:57

In order to make a daily repeated notification, you need to use an every:"day" (or interval in minutes: every: 24*60) and a firstAt property with the date when the notification will be triggered for the first time. Try this code

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

In their codebase is shown (commented) that you could achieve this by doing this

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

Now, if you have to send a notification every day at the same time you could either:

1 - schedule tenths of notifications and check each time a user opens your app

2 - re-schedule a notification each time the user opens up a notification already received.

In order to make a daily repeated notification, you need to use an every:"day" and a firstAt property with the date when the notification will be triggered for the first time.

Note: Unlike cordova plugin in Ionic 3 firstAt property needs to be wrapped in trigger property. You can find more information in Ionic Local Notification Documentation.

Try this code

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"}
  }
]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!