ios - 我怎样才能让 UNNotificationRequest 立即弹出?

标签 ios swift notifications ios10 unnotificationrequest

我正在使用 UNNotificationRequest,我希望在单击按钮时立即弹出通知。但在这种情况下,它会在我退出应用程序时出现。 这是我的代码

@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)
}

最佳答案

当问题是关于更现代的 UNNotificationRequest 时,Max 的回答是针对已弃用的 UILocalNotification

正确答案是传递 nil。根据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.

所以在你的代码中:

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)

关于ios - 我怎样才能让 UNNotificationRequest 立即弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637029/

相关文章:

ios - Swift - CoreData 故障

swift - 谷歌语音到文本的 swift 集成

ios - 谁能帮助我理解为什么我的 PFUser 子类在 save() 上崩溃?

swift - 根据登录的用户从 firebase 加载数据

azure - Azure 服务总线通知中心的 ios 通知模板

ios - 如何防止 UIWindow View 自动旋转

objective-c - NSArray 反向索引排序

android - ios/android 应用的 Laravel 密码加密/解密机制

Java Android - Redmi 3 (MIUI) - 无法更改通知图标?

php - 使用 jquery php 自动发送电子邮件通知