简体   繁体   中英

Notifications in Ionic 2

I have gone through many posts.

Its bit confusing to understand that does ionic 2 app send local-notification even if my-app-is-killed-by-user or not ?

I have below scenario.

Assuming that app-is-killed-by-user

Can we run the app in background (like after 24 hours check if some conditions are true by manipulating local/cloud storage) then send local notification ?

I am new to ionic 2. So can anyone let me know that above scenario is possible ?

If not possible then let me know that can we manipulate (get and analyse) local-storage via push notification server (assuming that app-is-killed-by-user) ?

Both are possible. I'll tell you how i would do them and not share code (it can be long), i'll give you the steps and you can figure it out.

For the first scenario you'll need LocalNotification plugin , LocalStorage , Platform import from 'ionic-angular' and MomentJS (not required, but highly recommended).

  • You'll need to create a service, at least it's good so you don't need to put everything in your app.components .
  • You can use 3 of the Platform methods to verify and set the LocalNotification , they're .pause , .resume and .ready .
  • Pause will check if the user has left the app, but it's still running on background.
  • Resume will check if the user has come back to the app, but'll not trigger if the app was killed.
  • Ready will trigger when platform's ready, you'll use this to check if the app was killed and is starting for "the first time".
  • You'll need MomentJS to manipulate time, since it's easier to add days/hours to your current datetime.
  • LocalNotification accepts both ISO string and UNIX timestamp to schedule a notification.
  • Inside your created service you'll have methods to schedule and cancel a LocalNotification, check the plugin wiki for those.
  • On the pause method you'll call the schedule method and schedule a localNotification having Moment add 1 day to your current time: Moment().add(1, 'day').unix() and use the result in the 'at' of your schedule.
  • This'll return a promise with the data of your notification and it's id, save this id in your localStorage like this.storage.set('Daily', ReturnedID);
  • When the app is ready or resume you'll cancel the notification.
  • So basically that's it, when going off you save a notification, when going in you cancel it.

The second scenario is a little bit harder and you'll need a back-end for this since you'll need to keep checking the time that user has gone off the app. You'll need only MomentJS , the Platform import, you push service plugin and a way to connect to your database (via Http or plugin, if it's a service like Firebase).

  • The steps are the same for the first scenario, the new service, the platform calling this service methods, but now will add some methods for you push plugin, so when you receive a push you can show and manipulate data.
  • Most push services (and localNotification) have a Data attribute where you can share some data with the user, in your case it'll be the data you want to safe in the localStorage.
  • You'll need a token of that user so you can send a push to him, see your push docs to see how you can get that token and save it to the user database register/node.
  • When the user leave the app you'll store in your database the time you want the user to receive that push (i'm assuming you want to do the same as the first scenario, but in a different way).
  • When the user come back to the app, you'll detele that time register.
  • The tricky part: you'll need your back-end to go through every user register and get the time they need to receive the push, so if the time is < the current time you'll send the push and delete that register.
  • In the app when the user receive the push a function'll save the data to localStorage, manipulate, ready or everything you want.

Sorry i can't share some code, but if you need furter explanation or ideas just ask. Hope this helps :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM