首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎样才能使UNNotificationRequest立即流行?

我怎样才能使UNNotificationRequest立即流行?
EN

Stack Overflow用户
提问于 2017-04-26 14:26:08
回答 2查看 4.8K关注 0票数 11

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

代码语言:javascript
复制
@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)
}
EN

回答 2

Stack Overflow用户

发布于 2019-04-08 16:34:53

当这个问题涉及到更现代的UILocalNotification时,Max的答案是反对的UNNotificationRequest

正确的答案是传递nil。根据requestWithIdentifier:content:trigger:

trigger 导致传递通知的条件。指定“零”立即传递通知。

所以在你的代码中:

代码语言:javascript
复制
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
票数 15
EN

Stack Overflow用户

发布于 2017-04-26 14:34:12

https://developer.apple.com/reference/uikit/uilocalnotification

如果应用程序在系统传递通知时是最重要和可见的,则调用应用程序委托的应用程序(_:didReceive:)来处理通知。使用提供的UILocalNotification对象中的信息来决定采取什么操作。该系统不显示任何警报,徽章应用程序的图标,或播放任何声音,当应用程序已经是最前面。

这是正常的行为。此外,为什么您需要通过点击按钮触发本地通知?为什么不实现所需的功能时,按钮被点击,而不是通过通知?

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43637029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档