2016-08-11 11 views
5

forループで一連の通知をスケジュールしようとしています。スケジューリング後、すべての保留中の通知を読みます(テスト目的のみ)。UNUserNotificationCenter - forループの1つのイベントのみをスケジュールするswift 2.3、iOS10、

(forループから呼び出さ)スケジューリング:

func scheduleNotification (event : Meeting, todaysBadgeCounter: Int) { 
if #available(iOS 10.0, *) { 

     let minutesBefore = CallIn.Settings.notifyNumberOfMinutesBeforeEvent 
     let notifyBefore = UNNotificationAction(identifier: NotificationActions.NotifyBefore.rawValue, title: "Notification", options: []) 
     let category = UNNotificationCategory(identifier: "CALLINNOTIFICATION", actions: [notifyBefore], intentIdentifiers: [], options: []) 


     UNUserNotificationCenter.currentNotificationCenter().setNotificationCategories([category]) 
     let content = UNMutableNotificationContent() 

     content.title = NSString.localizedUserNotificationStringForKey(event.title, arguments: nil) 
     if(minutesBefore <= 1){ 
      content.body = NSString.localizedUserNotificationStringForKey("Your \(event.title) is about to start", arguments: nil) 
     }else{ 
      content.body = NSString.localizedUserNotificationStringForKey("You have \(event.title) in \(Int(minutesBefore)) minutes", arguments: nil) 

     } 
     content.sound = UNNotificationSound.defaultSound() 

     //interval in seconds from current point in time to notification 
     let interval : NSTimeInterval = NSTimeInterval(secondsFromNowTo(event.startTime.dateByAddingTimeInterval(-minutesBefore * 60))) 


     let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false) 
     let request = UNNotificationRequest.init(identifier: "sampleRequest", content: content, trigger: trigger) 

     //only schedule in the future 
     if(interval > 0){ 
      UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: { (error) in 
       // handle the error if needed 
       log.error(error?.localizedDescription) 
       print("SCHEDULING >=iOS10:", event.title, ", interval:", interval) 
      }) 
     } 
} 

読み出し:後者の方法で

func printScheduledNotifications() { 
    if #available(iOS 10.0, *) { 
     print("printing scheduled notifications >=iOS10") 
     UNUserNotificationCenter.currentNotificationCenter().getPendingNotificationRequestsWithCompletionHandler({ (notifications) in 
      print("count", notifications.count) 
      for notification in notifications{ 

       print(notification.description) 
      } 
     }) 
    } 

"通知" はUNNotificationRequest配列が、この配列のリターンの数であります常に1とスケジュールされたイベント - 私はスケジュールメソッドに供給しているリストの最後のイベントです。

スケジューリング中に予定されているすべての予定と間隔(今後の予定)を印刷していますが、予定したいすべての予定のログが表示されますが、どういうわけか、イベントは前のイベントを上書きします。私は間違って何をしていますか?

+0

どこにでもブレークポイントを設定すると、UNUserNotificationsCenterのインスタンスがどこからでも異なることがわかります。これはおそらく問題です。私はそれを1つのインスタンスにする必要があります。それはなぜ今のようなものではないのですか? –

+0

シングルトンを作成しましたが、今は同じインスタンスですが、まだ役に立ちませんでした。 –

答えて

6

問題は実際にはUNUserNotificationCenterではなく、私のコードでforループの繰り返し全体を通して同じであった要求の識別子です。一意のIDを追加すると、うまくいきました。

let identifier = NSString.localizedUserNotificationStringForKey("sampleRequest\(event.title)", arguments: nil) 

いつものように、問題はそれよりもはるかに容易でした。

+0

これはまさに私の問題でした:) – ToddB

関連する問題