Scenario is: I need to fire local notifications for an array of objects, each after a fixed interval of time in seconds here is the code which works only for 1 interval if i provided a fix number of seconds
func DeliverMyMessage(_ dict: EnFact, _ index: Int){
let content = UNMutableNotificationContent();
content.body = dict.Content!;
content.categoryIdentifier = "learn";
content.userInfo = ["dictid": "Random Fact -\(dict.ID!)- \(dict.Reference!)" ];
content.sound = UNNotificationSound.default();
content.badge = 1;
print(secArray[index]);
var ui = TimeInterval.init(secArray[index]);
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
cnter.add(request, withCompletionHandler: { (error) in
if let error = error {
// Something went wrong
print(error);
}
print("delivered");
})
}
Notice : UNTimeIntervalNotificationTrigger(timeInterval: 5 if i provide a fix interval, lets say 5 - all notifications which i fired from an array, fires at after 5 seconds,
then if i try to give a calculated difference in seconds in a loop, the notifications doesn't seems to fire
func DeliverMyMessage(_ dict: EnFact, _ index: Int){
let content = UNMutableNotificationContent();
content.body = dict.Content!;
content.categoryIdentifier = "learn";
content.userInfo = ["dictid": "Random Fact -\(dict.ID!)- \(dict.Reference!)" ];
content.sound = UNNotificationSound.default();
content.badge = 1;
print(secArray[index]);
var ui = TimeInterval.init(secArray[index]);
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5 + ui , repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
cnter.add(request, withCompletionHandler: { (error) in
if let error = error {
// Something went wrong
print(error);
}
print("delivered");
if(index < ( self.dictArray.count - 1) ){
self.DeliverMyMessage(self.dictArray[index + 1], index + 1);
}else{
print("all delivered");
}
})
}
it states all delived but its not, i am using iOS 11.3 XCode 9.3 swift 4, I am stuck on it as this is the last part i need to do in the project - any help is greatly appreciated