0

I have a SwiftUI Timer App and I want the user to get notified when the timer runs out. I currently do this with just a local notification.

let content = UNMutableNotificationContent()
content.title = String(localized: "proTimer")
content.body = String(localized: "\(name)IstAbgelaufen")
content.sound = UNNotificationSound(named: UNNotificationSoundName("\(sound).wav"))

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: endDate!.timeIntervalSince(Date.now), repeats: false)
let request = UNNotificationRequest(identifier: id.uuidString, content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request)

Unfortunately, if the user has enabled Do Not Disturb, this notification will no longer be delivered. What can I do against it? Is there something like "urgent notifications"?

James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

2

yes, you need to enable "Critical alert" property in UNNotificationSetting while configuring local notification.

Documentation -> https://developer.apple.com/documentation/usernotifications/unnotificationsettings/2963116-criticalalertsetting

check this link UNNotification Local Notification and Do Not Disturb

Chandaboy
  • 1,302
  • 5
  • 10
  • 4
    Critical alerts could be a bit extreme. There are also [time sensitive](https://developer.apple.com/documentation/usernotifications/unnotificationinterruptionlevel/timesensitive) notifications; the user can still disable them, but this is their choice. – Paulw11 Aug 07 '23 at 10:23
0

You could also configure and enable an AVAudioSession and play a sound using AVPlayer. You just need to make sure your app gets its computing power when the time is up.

This would also work on phones with the silence-switch enabled.

The only hacky thing here is to wake the phone and let your sound play.

Instead I'd think an app should look for the Do-Not-Disturb mode and simply warn, if it's enabled.

Anticro
  • 685
  • 4
  • 12