2017-01-04 66 views
0

我在我的應用程序中使用本地通知來向用戶發送緊急消息。用戶收到推送通知會發生什麼情況,然後創建一個本地通知並在60秒後觸發,時間間隔爲60秒。這很有效,緊急通知按預期每60秒觸發一次。本地通知UNTimeIntervalNotificationTrigger triggerWithTimeInterval每隔1分鐘觸發一次如何停止

本地通知明星每隔一分鐘發射一次。但我想 阻止他們。你能建議我如何處理這個問題嗎?

在iOS 9上,我們完全沒有遇到這個問題,即使通知也會在一夜之間重複觸發,所以我想這可能與iOS 10有關?

我用它來創建通知的代碼如下:

let content = UNMutableNotificationContent() 
      content.body = NSString.localizedUserNotificationString(forKey: notificationMessage, arguments: nil) 

      content.badge = 1 
      let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true) 
      let request = UNNotificationRequest.init(identifier: "", content: content, trigger: trigger) 
      center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in 
       if error == nil { 
        print("add NotificationRequest succeeded!") 
//     trigger.timeInterval. 
       } 
      }) 
+0

FUNC redirectLogToDocuments(){ 讓allPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,真) 讓documentsDirectory = allPaths.first! 讓pathForLog = documentsDirectory.appendingFormat(「/ applog.txt」) freopen(pathForLog.cString(using:String.Encoding.ascii)!,「a +」,stderr) } –

回答

0

我發現缺少點。

let content = UNMutableNotificationContent() 
    content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil) 
content.body = NSString.localizedUserNotificationString(forKey: notificationMessage, arguments: nil) 
content.sound = UNNotificationSound.default() 
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber? 
    content.categoryIdentifier = "com.elonchan.localNotification" 
// Deliver the notification in five seconds. 
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: false) 
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger) 
// Schedule the notification. 
center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in 
    if error == nil { 
     print("add NotificationRequest succeeded!") 
     center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"]) 
    } 
})