场景是:我需要为一个对象数组发出本地通知,每个对象经过固定的时间间隔(以秒为单位) 如果我提供了一个固定的秒数
,这里的代码只能工作1个时间间隔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");
})
}
注意:UNTimeIntervalNotificationTrigger(timeInterval:5 if i 提供一个修复间隔,让我们说5 - 我解雇的所有通知 从数组中,在5秒后触发,
然后,如果我尝试在循环中给出计算的秒数差异,则通知似乎不会触发
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");
}
})
}
它表示所有已提供但不是,我使用iOS 11.3 XCode 9.3 swift 4,我被困在它上面,因为这是我在项目中需要做的最后一部分 - 非常感谢任何帮助