Reputation: 35
I'm doing an app in Swift 3 for iOS 10. I have done an UNNotificationContent to simulate a call in my app but it disappears approximately five seconds after being launched. I need to keep it more seconds, while the "calling" is in process. I want to keep the local notification until I call the removeAllPendingNotificationRequests method. Can I do it?
This is my code now:
let content = UNMutableNotificationContent()
content.body = "\(userName) is calling..."
content.sound = UNNotificationSound(named: "sound_call.wav")
content.badge = 1
let request = UNNotificationRequest(identifier: "notification", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request)
Thanks!
Upvotes: 1
Views: 572
Reputation: 1477
Try this:
let content = UNMutableNotificationContent()
content.body = "\(userName) is calling..."
content.sound = UNNotificationSound(named: "sound_call.wav")
content.badge = 1
// choose a random identifier
let id = UUID().uuidString
let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
// add our notification request
UNUserNotificationCenter.current().add(request)
print("Notified!", title)
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [id])
print("Notification removed!", id)
}
Answer found here: https://stackoverflow.com/a/68773924/46443
Upvotes: 0
Reputation: 4480
There's no way to control how long the notification stays on the screen. That's controlled by the system.
Upvotes: 1