WatchOS 4中的通知

时间:2018-08-02 01:34:08

标签: swift unusernotificationcenter watch-os-4

我正在尝试找出是否可以解决我的问题。每当RSSI达到20英尺时,我都想推送本地通知。我的代码有效,但在需要时不会触发通知。预先感谢!

大约需要5分钟才能收到通知...

func centralManager(_ Central:CBCentralManager,didDiscover外设:CBPeripheral,AdvertiseData:[String:Any],rssi RSSI:NSNumber){

    if peripheral.name?.range(of:"iPhone") != nil {

        print(RSSI)

        switch RSSI as! Int {
        case let x where x <=  -75:
            print("20ft")

            self.distance.setText("within 20ft")

                let content = UNMutableNotificationContent()
                content.title = "ALERT !"
                content.body = "well"
                content.sound = UNNotificationSound.default()
                // Time
                var trigger: UNTimeIntervalNotificationTrigger?
                trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
                // Actions
                let snoozeAction = UNNotificationAction(identifier: "Track", title: "Track", options: .foreground)

                let category = UNNotificationCategory(identifier: "UYLReminderCategory", actions: [snoozeAction], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction)
                let categories = Set<AnyHashable>([category])

                self.center.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())

                content.categoryIdentifier = "UYLReminderCategory"

                let identifier: String = self.stringUUID()

                let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

                self.center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
                    if error != nil {
                        print("Something went wrong: \(error!)")
                    }
                })





        case let x where x <  -60:
            print("5ft")
            self.distance.setText("within 5ft")
            session.sendMessage(["com.peard.track":distance!],replyHandler: { reply in
                // success handle reply if expecting a reply
                print(reply)
            })



        case let x where x >=  -60:
            print("3ft")
            self.distance.setText("within 3ft")
            session.sendMessage(["com.peard.track":distance!],replyHandler: { reply in
                // success handle reply if expecting a reply
                print(reply)
            })


        default:
            print("Not sure")
        }

        print("exists")
    } else{
        print("Does Not Exist")
    }

}

0 个答案:

没有答案