【问题标题】:how to implement multiple local notification not a single notification for swift 3如何为swift 3实现多个本地通知而不是单个通知
【发布时间】:2017-05-03 09:13:24
【问题描述】:

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

    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 和不同时间将此代码与多个本地通知一起使用 谢谢。

【问题讨论】:

  • 那么,您找到解决方案了吗? :)

标签: notifications swift3 ios10 localnotification


【解决方案1】:

为每个通知使用不同的请求标识符(否则您只能看到最后一个通知)。 在上面的示例中,请确保请求标识符“UUID().uuidString”包含每个通知请求的唯一值。

【讨论】:

    【解决方案2】:

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

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

    希望这会有所帮助。 :)

    【讨论】:

    • “确保为每个通知设置唯一标识符”这是我的错误... +1
    【解决方案3】:

    用不同的参数调用这个函数。就像我在应用程序进入后台时调用的那样

      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
                }
            })
        }
    

    【讨论】:

      【解决方案4】:

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

      我认为我几乎遇到了完全相同相同的问题,在我尝试跟踪待处理的通知后,我注意到唯一添加的通知是最后一个请求的通知。那是因为你正在调用scheduleLocal() 函数:

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

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

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

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

      【讨论】:

        【解决方案5】:

        如果您想使用多个本地通知,则必须为每个请求使用不同的标识符。

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

        使用您对每个请求的不同标识符更改“已更改库存”。

        您可以对多个请求使用循环。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多