gpt4 book ai didi

iphone - 取消 UILocalNotification

转载 作者:行者123 更新时间:2023-12-03 18:11:31 26 4
gpt4 key购买 nike

我的 UILocalNotification 有问题。

我正在用我的方法安排通知。

- (void) sendNewNoteLocalReminder:(NSDate *)date  alrt:(NSString *)title
{
// some code ...
UILocalNotification *localNotif = [[UILocalNotification alloc] init];

if (localNotif == nil)
return;

localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.alertBody = title;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"id"];
localNotif.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}

它工作正常,我正确地收到了通知。问题是我什么时候应该取消通知。我正在使用这个方法。

- (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE
{
[[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ????
}

我不知道在这里做什么,但我的问题是:

我如何知道应该删除哪个 UILocalNotification 对象?
有没有办法列出所有通知?

我唯一拥有的是我应该删除哪个提醒的 ID。
我正在考虑将 UILocalNotification 对象保存在我的“Note”对象中并以这种方式获取它,当我保存到 SQLite 数据库时序列化该对象等等......有没有更聪明的方法?

最佳答案

我的解决方案是使用 UILocalNotification userInfo 字典。事实上,我所做的是为我的每个通知生成一个唯一ID(当然这个ID是我稍后可以检索到的),然后当我想要时要取消与给定 ID 关联的通知,我只需使用数组扫描所有可用通知:

[[UIApplication sharedApplication] scheduledLocalNotifications]

然后我尝试通过调查ID来匹配通知。例如:


NSString *myIDToCancel = @"some_id_to_cancel";
UILocalNotification *notificationToCancel=nil;
for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) {
notificationToCancel=aNotif;
break;
}
}
if(notificationToCancel) [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

我不知道这种方法相对于归档/取消归档方法是否更好,但它确实有效,并且限制数据仅保存为 ID。

编辑:缺少一个刹车

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

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