我想向用户显示在调用本地通知之前剩余的时间。我可以显示时间,但不会更新。有没有办法让本地通知中的剩余时间更新?这是代码的一部分。
func notificationCall() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
if granted {
let content = UNMutableNotificationContent()
content.title = "Ready for the QOTD"
content.body = "You have 1 min to answer the question"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 86400, repeats: false)
let request = UNNotificationRequest(identifier: "myTrigger", content: content, trigger: trigger)
center.add(request)
如果我打印出 timeInterval,我会收到原始时间 86400,而不是更新它。我尝试使用计时器与通知同步,但如果应用程序进入后台或被终止,您会遇到许多其他问题。任何建议或解决方案将不胜感激。
最佳答案
UIApplication 的属性名为 scheduledLocalNotifications
NSMutableArray *notifications = [[NSMutableArray alloc] init];
[notifications addObject:notification];
app.scheduledLocalNotifications = notifications;
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
if ([uid isEqualToString:uidtodelete])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
if ([localNotification.alertBody isEqualToString:savedTitle]) {
NSLog(@"the notification this is canceld is %@", localNotification.alertBody);
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
希望这有助于获取通知详情,否则你必须将通知详情保存在userdefault中,然后将安排的时间与当前设备时间进行比较,结果将是剩余时间
关于ios - 有没有办法获得引用以查看本地通知中剩余的剩余时间,并更新它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648350/