Swift   发布时间:2019-10-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了通知 – 如何实现多个本地通知而不是swift 3的单个通知大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用单一通知,这是我的代码: 
这是用于注册本地通知>>>    
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的单个通知所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。