gpt4 book ai didi

ios - 取消 UILocalNotification

转载 作者:行者123 更新时间:2023-11-28 19:50:00 28 4
gpt4 key购买 nike

我目前正在尝试取消与我的数据模型中的对象关联的特定 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com