通知 – 如何实现多个本地通知而不是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 :)函数中传递的标识符对于每个通知都是不同的.

希望这可以帮助.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...