取消 UILocalNotification

Canceling UILocalNotification

我目前正在尝试取消与我的数据模型中的对象关联的特定 UILocalNotifications。为此,每个数据对象都有一个唯一标识符,即 NSUUID.

创建 UILocalNotification:

/* Set UILocalNotification */
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = date;
        localNotification.alertBody = self.mytextfield.text;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.applicationIconBadgeNumber = 1;
        NSLog(@"making a notification: %@",[r.uniqueId UUIDString]);
        [localNotification.userInfo setValue:[r.uniqueId UUIDString] forKey:@"uid"];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

然而,当我去删除通知并打印出每个通知内容时,通知的 alertbody 打印正确但唯一标识符不知何故丢失了。这个实现有什么问题?

取消 UILocalNotification:

Obj *r = (Obj *)[self.objects[indexPath.section] objectAtIndex:indexPath.row];

    /* Cancel UILocalNotification */
    NSArray *scheduleReminders = [[UIApplication sharedApplication] scheduledLocalNotifications];
    NSLog(@"scheduled.count: %i",(int)scheduleReminders.count); // Correctly prints 3
    for (int i = 0; i < scheduleReminders.count; i++)
    {
        UILocalNotification *notification = scheduleReminders[i];

        NSDictionary *userInfoCurrent = notification.userInfo;
                NSLog(@"searching through reminders: %i %@ %@ %@",(int)i,[userInfoCurrent objectForKey:@"uid"], notification.alertBody); // Alert body prints correctly

        if ([[userInfoCurrent valueForKey:@"uid"] isEqualToString:[r.uniqueId UUIDString]])
        {
            NSLog(@"found it");
            //Cancelling local notification
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            break;
        }
    }

问题是 userInfo 默认是 "nil"。您应该分配自己的 NSDictionary,设置 uid,然后将此字典设置为 localNotification.userInfo.