1

我想向用户显示调用本地通知之前的剩余时间。我可以显示时间,但它不会更新。有没有办法让本地通知中剩余的更新时间?这是代码的一部分。

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 的原始时间,而不是更新它。我尝试使用计时器与通知同步,但如果应用程序进入后台或被终止,您会遇到许多其他问题。任何建议或解决方案将不胜感激。

4

1 回答 1

0

UIApplication 有一个名为scheduleLocalNotifications的属性

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 中,然后将安排的时间与当前设备时间进行比较,结果将是剩余时间

于 2016-09-23T03:27:21.127 回答