通知 – 如何实现多个本地通知而不是swift 3的单个通知

通知 – 如何实现多个本地通知而不是swift 3的单个通知,第1张

概述我使用单一通知,这是我的代码: 这是用于注册本地通知>>> func registerLocal() { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in 我使用单一通知,这是我的代码:
这是用于注册本地通知>>>
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和不同时间的多个本地通知
谢谢 .

您可以使用不同的dateComponents多次调用func scheduleLocal()来安排在不同的日期.或者,您可以将一组日期传递给此函数并运行循环以根据这些日期安排通知.

只需确保您在UNNotificationRequest(IDentifIEr:,content:,trigger :)函数中传递的标识符对于每个通知都是不同的.

希望这可以帮助. 总结

以上是内存溢出为你收集整理的通知 – 如何实现多个本地通知而不是swift 3的单个通知全部内容,希望文章能够帮你解决通知 – 如何实现多个本地通知而不是swift 3的单个通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089956.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存