首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >取消UILocalNotification

取消UILocalNotification
EN

Stack Overflow用户
提问于 2015-04-18 02:10:43
回答 1查看 199关注 0票数 0

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

创建UILocalNotification:

代码语言:javascript
复制
/* 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将被正确打印,但是唯一的标识符不知何故丢失了。这个实现有什么问题?

Cancel UILocalNotification:

代码语言:javascript
复制
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;
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-18 05:45:34

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29712127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档