取消UILocalNotification

时间:2015-04-18 02:10:43

标签: ios objective-c 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;
        }
    }

1 个答案:

答案 0 :(得分:3)

问题是userInfo是&#34; nil&#34;默认情况下。您应该分配自己的NSDictionary,设置uid,然后将此字典设置为localNotification.userInfo。