ios - 大量本地通知swift 3

标签 ios swift notifications local

我有一系列字典。字典包含不同栏的名称/经度/和纬度。

我想为我应用中的每个栏创建基于位置的通知。

这是我正在使用的功能,但出于某种原因,它只为列表中的第一个栏创建通知。其余的不起作用。

知道这有什么问题吗?我不确定 content.badge 的用途。

// Register location based notifications for all bars
func setupNotifications(){
    if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse){


        for i in 0...(allBars.count - 1){
            let lat = (allBars[i])["Latitude"] as? Double
            let long = (allBars[i])["Longitude"] as? Double
            let name = (allBars[i])["Name"] as! String

            let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: lat!, longitude: long!), radius: 60, identifier: "\(name)")

            region.notifyOnEntry = true
            region.notifyOnExit = false

            let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = "Rate \(name) in the app!"
            content.badge = 1
            content.sound = UNNotificationSound.default()

            let request = UNNotificationRequest(identifier: "LN\(name)", content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request) {(error) in
                if let error = error{
                    print("Need notification permissions... \(error)")
                }
                else{
                    //success
                }
            }
        }
    }
}

最佳答案

import UserNotifications

class ViewController: UIViewController,UNUserNotificationCenterDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    print("UNUserNotificationCenter")

    UNUserNotificationCenter.current().requestAuthorization(options: [.sound,.alert,.badge], completionHandler: { didallow, error in

     })
}

@IBAction func btn(_ sender: UIButton) {
    let content = UNMutableNotificationContent()

    content.title = "the 5 sec are up"
    content.subtitle = "they are up now"
    content.body = "the 5 sec are realy up now"
    content.badge = 1

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false)

    let request = UNNotificationRequest(identifier: "time done", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("Handle push for foreground")

    print("\(notification.request.content.userInfo)")
}


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Handle push for background or close")

    print("\(response.notification.request.content.userInfo)")

    completionHandler()
}

关于ios - 大量本地通知swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151431/

相关文章:

objective-c - 选择行时 UIPickerView 不会被删除

arrays - 要求数组是 Swift 协议(protocol)中的最小大小

ios - 使用 urlsession 下载文件

重启后android通知消失

ios - Swift 如何获得 UIScrollView 动态高度

ios - 我的 UICollectionView 不显示图像

ios - Swift 框架不适用于命名为 'Debug' 或 'Release' : No such module 以外的构建配置

ios - 在 CALayer 上使用 hitTest - 如何找到哪个层被击中(Swift)?

swift - 惯用的 Swift 多重通知?

javascript - JS 根据用户设置发送通知