ios - 取消 UILocalNotification

标签 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;
        }
    }

最佳答案

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

关于ios - 取消 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712127/

相关文章:

ios - 当用户快速点击一个单元格时触发一个 Action

ios - 将 UILabel 添加到 UIToolbar

objective-c - NSString 到类实例变量

ios - UIView 底边模式

notifications - iOS 5 : Can I have my app "In Notification Center" by default?

ios - 当 slider 按钮超过最大值的大约 60% 时,UISlider 最大轨道图像使用最小轨道图像

objective-c - 将数组存储到 SQLite 数据库中

objective-c - 如何获取目录中的dylib版本信息

iphone - UILocalNotification 调用延迟

ios - 在本地通知上发送带有自定义声音的触觉