I'm working with UNNotificationRequest
and I want that the notification popup immediately when I click on the button. but in this case it appears when I quit the application.
Here's my code
@IBAction func shortNotifBtn(_ sender: Any) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Late wake up call"
content.body = "The early bird catches the worm, but the second mouse gets the cheese."
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}
Max's answer is for the deprecated UILocalNotification
when the question is regarding the more modern UNNotificationRequest
.
The correct answer is to pass nil
along. According to the documentation for UNNotificationRequest
's requestWithIdentifier:content:trigger:
trigger
The condition that causes the notification to be delivered. Specify nil to deliver the notification right away.
So in your code:
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With