我正在显示一条通知,要求用户在接下来的10秒钟内采取一些措施,否则它将有所作为。
我想做的是每秒更新通知的文本,以便用户知道他还有多少时间可以采取行动,例如。 Device not connected. Restarting watcher in X seconds
其中X每秒更新为0。
这是我到目前为止所拥有的:
let centre = UNUserNotificationCenter.current();
let content = UNMutableNotificationContent();
var i:Int = 10;
content.title = NSString.localizedUserNotificationString(forKey: "This is the title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Device not connected. Restarting watcher in \(i) seconds", arguments: nil)
content.categoryIdentifier = "Category";
content.sound = UNNotificationSound.default;
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false));
let completeAction = UNNotificationAction.init(identifier: "Complete", title: "Complete", options: UNNotificationActionOptions())
let categories = UNNotificationCategory.init(identifier: "Category", actions: [completeAction], intentIdentifiers: [], options: [])
centre.setNotificationCategories([categories]);
centre.add(request, withCompletionHandler: nil);
let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
i-=1;
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(10)) {
timer.invalidate();
centre.removeDeliveredNotifications(withIdentifiers: ["FiveSecond"]);
};
}
有可能做到吗?