我的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数据库时序列化该对象,等等。有没有更聪明的方法?
发布于 2019-05-19 12:53:03
Swift 5:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)发布于 2011-02-03 05:37:43
我的解决方案是使用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。
编辑:缺少一个胸罩
发布于 2010-07-01 21:42:57
您可以从scheduledLocalNotifications获取所有计划通知的列表,也可以将其全部取消:
[[UIApplication sharedApplication] cancelAllLocalNotifications];https://stackoverflow.com/questions/3158264
复制相似问题