gpt4 book ai didi

ios - 在 iOS 中单击本地通知时不打开应用程序

转载 作者:搜寻专家 更新时间:2023-10-30 20:16:35 25 4
gpt4 key购买 nike

当我点击来自通知中心的本地通知时,我想阻止它打开我的应用程序,即当点击通知时,除了清除它之外什么都不应该发生。知道怎么做吗?

最佳答案

我知道这篇文章很老了,但我在尝试自己弄清楚如何做到这一点时发现了这个。我想我会更新以防其他人遇到它。

我正在研究 Project 21 Hacking With Swift 时,其中一项挑战要求您从通知中设置“稍后提醒我”选项。显然,在这种情况下打开应用程序是没有用的,所以我也想弄清楚。

这是我发现的方式其实很简单。注册类别时,您只需删除一段代码。

例如:

func registerCateories() {
let center = UNUserNotificationCenter.current()
center.delegate = self // Any messages get sent back to us

let show = UNNotificationAction(identifier: "show", title: "Tell me more", options: .foreground)
let remindMeLater = UNNotificationAction(identifier: "remindMeLater", title: "Remind me later...")
let category = UNNotificationCategory(identifier: "alarm", actions: [show, remindMeLater], intentIdentifiers: [], options: [])
center.setNotificationCategories([category])
}

您可以从本地通知中点击的两个选项是 showremindMeLater。注意 showoptions: .forground。这就是强制您的应用程序在单击时打开的原因。删除它(就像在 remindMeLater 中一样),你就解决了你的问题!

稍后在您的代码中,您可以根据 userNotificationCenter 函数中的 response.actionIdentifier 调用不同的函数,以使水龙头实际执行某些操作。

希望对您有所帮助!

关于ios - 在 iOS 中单击本地通知时不打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28019991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com