2017-04-22 26 views
2

我正在製作一個應用程序,使用圓形區域作爲地理圍欄。當手機處於活動狀態或應用程序處於打開狀態時,地理圍欄通知在模擬器和設備(運行10.3.1的iPhone 6)中均可正常工作。Geofence警報/本地通知沒有在觸發時喚醒鎖定的手機

在模擬器中,它工作正常;當用戶進入一個區域時,它會醒來,發出聲音並在鎖定屏幕上顯示警報。

在電話上,「didEnterRegion」代表電話是在進入該區域時進行的(我記錄了一些消息),但電話沒有發出警報並且沒有醒來。當我按下主頁按鈕一次後,我可以在鎖定屏幕上看到警報,但我希望它能立即醒來並立即顯示警報 - 就像我收到消息時一樣。它在模擬器中工作,所以我不知道什麼是錯的?它已經爲我工作了幾次,其中警報顯示在手機和手錶上,但95%的時間不工作 - 通知已生成,但只有在手動喚醒手機時纔會顯示。

如何解決這一問題?

這是我用來創建本地通知代碼:

 // https://blog.codecentric.de/en/2016/11/setup-ios-10-local-notification/ 


     let location = CLLocation(latitude: item.coordinate.latitude, longitude: item.coordinate.longitude) 
     GeoTools.decodePosition(location: location) { 
      (address, city) in 
      let content = UNMutableNotificationContent() 
      content.title = "Camera nearby!" 
      content.subtitle = item.id 
      content.body = "\(address), \(city)" 
      content.categoryIdentifier = Constants.notificationCategoryId 
      content.sound = UNNotificationSound.default() 
      content.threadIdentifier = item.id 

      // FIXME make action for clicking notification 

      let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.001, repeats: false) // FIXME HACK 

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

      let unc = UNUserNotificationCenter.current() 
      unc.removeAllPendingNotificationRequests() 
      unc.add(request, withCompletionHandler: { (error) in 
       if let error = error { 
        print(error) 
       } 
       else { 
        print("completed") 
       } 

      }) 

     } 

回答

1

這裏是我剛剛驗證了一些代碼醒來時,通知提出的設備:

let message = "CLRegion event" 
// Show an alert if application is active: 
if UIApplication.shared.applicationState == .active { 
    if let viewController = UIApplication.shared.keyWindow?.rootViewController { 
     showSimpleAlertWithTitle(nil, message: message, viewController: viewController) 
    } 
} 
else { 
    // Otherwise app is in background, present a local notification: 
    let content = UNMutableNotificationContent() 
    content.body = message 
    content.sound = UNNotificationSound.default() 
    content.categoryIdentifier = "message" 

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false) 
    let request = UNNotificationRequest(identifier: "com.foobar", content: content, trigger: trigger) 
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
} 

真的是唯一差異是我不會撥打removeAllPendingNotifications(),所以如果您必須刪除通知,我不知道removePendingNotificationRequests(withIdentifiers identifiers: [String])可能更準確嗎?

相關問題