如何在swift3上关闭app后显示通知?

时间:2017-03-29 15:21:41

标签: swift3 notifications xcode8

我想在应用程序关闭swift 3时显示通知。是否可能,  怎么做?你能告诉我吗?

//获取数据        func getData(){

        let url=URL(string:"http://MyGPS/service.php")
        do {
            let allContactsData = try Data(contentsOf: url!)
            let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
            if let arrJSON = allContacts["mygps"] {
                for index in 0...arrJSON.count-1 {                    
                    vibration.append(aObject["vibration"] as! String)                  

                }
            }           
        }
        catch {

        }
}

//显示通知

    func notification(){            

        let content = UNMutableNotificationContent()
        content.title = "GPS alert message"
       // content.subtitle = "Do you know?"
        content.body = "Your GPS detect vibration!!"
        content.badge = 1
        content.sound = UNNotificationSound.default()
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

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

    }

//调用函数

       func someBackgroundTask(timer:Timer) {
            DispatchQueue.global(qos: DispatchQoS.background.qosClass).async {
                //print("do some background task")
                 self.getData()
                DispatchQueue.main.async {        
                    //self.updatMarker()
                }
            }
        }

//使用计时器

    Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) {
                    timer in

                    self.someBackgroundTask(timer: timer)
                }

0 个答案:

没有答案