Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make UNNotificationRequest pop immediately?

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)
}
like image 638
Ghassen Khelif Avatar asked Apr 26 '17 14:04

Ghassen Khelif


1 Answers

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)
like image 158
Michael Dautermann Avatar answered Oct 13 '22 13:10

Michael Dautermann