2017-01-04 97 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"]) 
    } 
})