4

I was trying to find an exact answer for the question: "what will happen to my scheduled notifications after the device is rebooted?".

I've used the UNUserNotificationCenter to schedule all the notifications and they will be triggered repeatedly on each day based on the scheduled time.

here is my written code snippet, and it works while the device is on.

func scheduleNotification() {
    let center = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = "This is the title"
    content.body = "The is the body"
    content.categoryIdentifier = "identifier"
    content.userInfo = ["info":"B"]
    content.sound = UNNotificationSound.default
    
    var dateComponents = DateComponents()
    dateComponents.hour = 0
    dateComponents.minute = 29
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    center.removeAllPendingNotificationRequests()
    center.add(request)
}
shadow of arman
  • 395
  • 1
  • 7
  • 16

1 Answers1

2

In short, it should remain, although I could not find an official apple documentation, it worked for me. The timer should survive a reboot. As long as the device is on when the relevant time arrives and the app is still installed, The notification will be fired. Of course permission for sending notifications is required.

This question was asked before with no answer - UNUserNotificationCenter notifications after device reboot

shadow of arman
  • 395
  • 1
  • 7
  • 16
CloudBalancing
  • 1,461
  • 2
  • 11
  • 22
  • Hi, I want to get notification when app is terminated and if app is not running then again I want notification every 72 hours. How can I achieve this ? Is it possible with local notification? Also, I am getting issue with local notification - I restart the device. I don't force-quit the app. And now the notifications don't trigger anymore. I need to open the app again. – Rajput Nancy Katal Jan 19 '23 at 17:59
  • Yep, this is uncharted territory in apple documentation with local notification. You have to register after a device reboot. What you should do is combine push and local notifications. On the app delegate `willTerminate` function trigger local notification and show it to the user, also make sure to register that fact in your backend. You will also need to register an app open on the backend. And apply the 72 hours logic from there - once it is passed - send a push notification to the user – CloudBalancing Jan 19 '23 at 20:03
  • Which means I cannot handle local notification after reboot device, so I need to implement Push Notification and need to send app open/app terminate status to backend? Also, I need to show local notification when app terminated and show Push notification after 72(logic will be at backend) if app is not running? Is that correct? – Rajput Nancy Katal Jan 19 '23 at 20:16
  • For my knowledge, I might be mistaken and things have changed – CloudBalancing Jan 19 '23 at 22:29