Onkar Singh
Onkar Singh

Reputation: 61

Schedule New Local Notification Every Hour

We are trying to develop an ionic app which will show local notification according at a scheduled interval according to users preference. The local notification should have a new content every time. This content will be saved locally. We need the app to work completely offline. These notifications should also work when the app is closed.

If someone can guide us onto how this can be done, it will be great.

Upvotes: 0

Views: 791

Answers (1)

Charis The Programmer
Charis The Programmer

Reputation: 959

You can make use of the Local Notification Plugin available from Ionic native, install it by running:

$ ionic cordova plugin add cordova-plugin-local-notification
$ npm install --save @ionic-native/local-notifications

Once installed, you can use it in whichever component you want with the following code:

import { LocalNotifications } from '@ionic-native/local-notifications';


constructor(private localNotifications: LocalNotifications) { }


// Schedule delayed notification
this.localNotifications.schedule({
   text: localStorage.getItem('localNotificationData'),
   trigger: {at: new Date(new Date().getTime() + 3600)},
   led: 'FF0000',
   sound: 'file://sound.mp3'
});

The Local notification will fire when the time in the date-time object you enter in the trigger field elapses. It will display whatever text you enter in the text field. In the above example, I set the text field value to whatever is returned from local storage. You can substitute that part with a hardcoded string or wherever you are saving your data. You will have to schedule a notification like this for each notification you want the user to receive. Once local notifications are set, they can run when the app is closed, no extra code needed.

Hope this helps!

Upvotes: 1

Related Questions