如何实现多个本地通知,而不是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和不同时间的多个本地通知使用此代码谢谢。

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

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

只需确保您在UNNotificationRequest(identifier:, content:, trigger:)功能中通过的标识符对于每个通知都不同。

希望这会有所帮助。:)

用不同的参数调用此功能。就像我在App上班后打电话

  func applicationDidEnterBackground(_ application: UIApplication) {
        setNotification(time: 25,identifier: "notific2")
        setNotification(time: 50,identifier: "notific3")
        setNotification(time: 90,identifier: "notific4")
    }

  func setNotification (time : Int,identifier : String)
    {
        let content = UNMutableNotificationContent()
        content.title = "Don't forget"
        content.body = "Buy some milk"
        content.sound = UNNotificationSound.default()
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(time),
                                                        repeats: false)
        let center = UNUserNotificationCenter.current()
        // Swift
        let request = UNNotificationRequest(identifier: identifier,
                                            content: content, trigger: trigger)
        center.add(request, withCompletionHandler: { (error) in
            if error != nil {
                // Something went wrong
            }
        })
    }

请注意,您的代码应按预期工作!但是,有一点陷阱。

我认为我几乎遇到了 Exact 同一问题,在我试图追踪待定通知后,我指出,唯一添加的通知是最后一个请求的通知。那是因为您在scheduleLocal()函数中打电话:

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

好吧,最好删除任何现有的通知提醒请求,这将有助于防止不必要的重复通知,但您应在调用scheduleLocal()之前只称其为

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
// now you could call this function many times...
scheduleLocal()

简单地,您不想在添加后直接删除每个待处理的通知,而是删除了整个以前的已待定通知并添加新通知。

如果要使用多个本地通知,则比为每个请求使用不同的标识符。

let request = UNNotificationRequest(identifier: "Stock Changed", content: content, trigger: nil)

更改"股票更改"每个请求都有不同的标识符。

您可以将循环用于多个请求。

最新更新