0

I want to set a local notification for a specific time, but I have concerns about it. For example, when I set the local notification for once and I say that I scheduled it and suddenly the user's phone turns off, what's gonna happen? I'm sure this notification data is just available in the memory and it will disappear when the user's phone turns off.

And I know every time I set the same date, I am gonna get more notifications at that time. I just want one notification, but as I told you, I don't know how to handle the possibilities for the user.

Here is my sample code :

func scheduleNotification(inSeconds : TimeInterval, completion : @escaping (_ Success : Bool) -> ()){
    let notif = UNMutableNotificationContent()
    notif.title = "New notification"
    notif.subtitle = "Cool!"
    notif.body = "I liked it!!!!"

    let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)
    let request = UNNotificationRequest(identifier: "myNotification", content:  notif, trigger: notifTrigger)

    UNUserNotificationCenter.current().add(request) { (error) in
        if error != nil{
            print(error!)
            completion(false)
        }else{
            completion(true)
        }
    }
}
2
  • To make it clear, you only want the notification to be shown once? Also, if your phone is off I believe you will never get the notification. That's to make sure that if you turn it off for a while, you won't get bombarded with notifications for that time.
    – MVZ
    Commented Nov 28, 2017 at 21:38
  • I mean I want to set one or two notifications just one time and after I open the app, I don't want to set again and again. And I know I can't do anything if the phone is off. I am just saying the possibilities and I'm trying to find what I can do about those problems.
    – jorjj
    Commented Nov 28, 2017 at 23:06

1 Answer 1

1

Local Notifications are stored between sessions. I just tested it by setting one and then switching my phone off and on. The notification still appeared. Of course you won’t get the notification if your phone is off.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.