ios - 有没有办法获得引用以查看本地通知中剩余的剩余时间,并更新它?

标签 ios swift uilocalnotification usernotifications

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

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/

相关文章:

ios - 在导航单元格时将 collectionView 单元格中的 textView 设为第一个响应者

ios - 如何在 Swift 中以编程方式将 HeaderView 从 nib 添加到 UiTableView

数组中多个日期的swift 2本地警报

ios - 每 n 天安排一次本地通知(时区安全)

iphone - UILocalNotification 不会触发

ios - 获取服务 'applinks' 的信息,应用 ID '<<NULL>>'

iphone - 在 iphone 模拟器上使用核心位置

iOS - 使用 RestKit 从 RESTful 服务获取对象

ios - 当滚动/索引超出数组范围时 UITableView 崩溃

iphone - 在 iPhone 中获取联系人信息的问题