Search code examples
ionic-frameworknotificationsionic3uilocalnotificationionic4

Ionic 3/4 - Daily Local Notifications not working


Local notifications don't work properly (tried it with ionic 3 & 4). The user can set a time in the app and turn the notifications on or off. When using the following code, I always get a notification at 01:00 am although I've set it to 17:30 or something else. I tried many variations, this is the last one:

const time = setsub.task_reminder_time.split(':');
    const now = new Date();
    console.log('now is', now);
    const pushDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), +time[0], +time[1], 0);
    const options = {
      id: 1,
      title: 'Time to plan your day!',
      text: `Hey you! It's time to get productive by planning your day!\nTap here to open DoDay! 🤓`,
      trigger: { firstAt: pushDate, every: ELocalNotificationTriggerUnit.DAY }
    };
    if (setsub.task_reminder) {
      this.notification.requestPermission();
      this.notification.schedule(options);
    } else {
      this.notification.clearAll();
    }

time is just a string containing the notification time in HH:mm I'm using an iOS device for testing.


Solution

  • 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
            }
          }
        }
    

    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.