gpt4 book ai didi

notifications - 如何实现多个本地通知而不是Swift 3的单个通知

转载 作者:行者123 更新时间:2023-12-04 09:10:21 25 4
gpt4 key购买 nike

我已经使用了单个通知,这是我的代码:
这是用于注册本地通知的>>>

    func registerLocal() {
let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yay!")
} else {
print("D'oh")
}
}
}

并且此功能可以安排本地通知>>>
func scheduleLocal() {
registerCategories()

let center = UNUserNotificationCenter.current()

// not required, but useful for testing!
center.removeAllPendingNotificationRequests()

let content = UNMutableNotificationContent()
content.title = "good morning"
content.body = "ttt123"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default()

var dateComponents = DateComponents()
dateComponents.hour = 23
dateComponents.minute = 18
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}

func registerCategories() {
let center = UNUserNotificationCenter.current()
center.delegate = self

let show = UNNotificationAction(identifier: "show", title: "Tell me more…", options: .foreground)
let category = UNNotificationCategory(identifier: "alarm", actions: [show], intentIdentifiers: [])

center.setNotificationCategories([category])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// pull out the buried userInfo dictionary
let userInfo = response.notification.request.content.userInfo

if let customData = userInfo["customData"] as? String {
print("Custom data received: \(customData)")

switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
// the user swiped to unlock; do nothing
print("Default identifier")

case "show":
print("Show more information…")
break

default:
break
}
}

// you need to call the completion handler when you're done
completionHandler()
}

现在如何在iOS 10和不同时间的多个本地通知中使用此代码
谢谢你 。

最佳答案

为每个通知使用不同的请求标识符(否则,您只会看到最后一个通知)。
在上面的示例中,确保请求标识符“UUID()。uuidString”包含每个通知请求的唯一值。

关于notifications - 如何实现多个本地通知而不是Swift 3的单个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203607/

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