0

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.

Onkar Singh
  • 61
  • 1
  • 5

1 Answers1

1

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!

  • As the title suggests we want to schedule a notification every hour or any similar interval. With you suggested solution we will have to loop over all the notification in our local storage and schedule each one separately. But if the user changes the preference then we will have to cancel all these notification and schedule each notification again. This does not seem like a good solution. – Onkar Singh Nov 14 '18 at 15:04