如何实现多个本地通知而不是Swift 3的单个通知 [英] how to implement multiple local notification not a single notification for swift 3

查看:115
本文介绍了如何实现多个本地通知而不是Swift 3的单个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have use the single notification , and this is my code: this is for register the local notification>>>

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

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

此功能可以安排本地通知>>>

and this function to schedule the local notification>>>

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和不同时间的多个本地通知中使用此代码 谢谢.

now how can i use this code with the multiple local notification with iOS 10 and different times thank you .

推荐答案

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

You can call the func scheduleLocal() multiple times with different dateComponents to schedule on different dates. Or you can pass a array of dates to this function and run a loop to schedule the notifications according to these dates.

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

Just make sure that the identifier you pass in UNNotificationRequest(identifier:, content:, trigger:) function is different for each notification.

希望这会有所帮助. :)

Hope this helps. :)

这篇关于如何实现多个本地通知而不是Swift 3的单个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆