f0r*_*0rz 48 iphone notifications cocoa-touch ios4
我的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];
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,我正确收到通知.问题是我应该取消通知.我正在使用这种方法.
- (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE
{
[[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ????
}
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做,但我的问题是:
我如何知道应删除哪个UILocalNotification对象?
有没有办法列出所有通知?
我唯一拥有的是我应删除的提醒ID.
我正在考虑将UILocalNotification对象保存在我的"Note"对象中并以此方式获取,当我保存到我的SQLite数据库时序列化对象等等......是否有更聪明的方法?
vig*_*o24 93
我的解决方案是使用UILocalNotification userInfo字典.实际上,我所做的是为每个通知生成一个唯一的ID(当然这个ID是我以后能够检索的),然后当我想取消与给定ID相关的通知时,我将只扫描所有使用数组的可用通知:
[[UIApplication sharedApplication] scheduledLocalNotifications]
Run Code Online (Sandbox Code Playgroud)
然后我尝试通过调查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];
Run Code Online (Sandbox Code Playgroud)
我不知道这种方法在存档/取消存档方面是否更好,但它可以工作并将数据限制为只保存为ID.
编辑:有一个缺少的braket
pro*_*rmr 57
您可以从scheduledLocalNotifications获取所有预定通知的列表,也可以全部取消:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Run Code Online (Sandbox Code Playgroud)
hie*_*pnd 13
我的解决方案是归档您使用NSKeyedArchiver安排的UILocalNotification对象并将其存储在某处(首选在plist中).然后,当您想要取消通知时,查找plist以获取正确的数据并使用NSKeyedUnarchiver取消归档.代码非常简单:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice];
Run Code Online (Sandbox Code Playgroud)
和
UILocalNotification *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Run Code Online (Sandbox Code Playgroud)
我的解决方案就是当你创建UILocalNotification那个时创建一个NSMutableDictionary并将该通知存储为key的值作为你的ID并将其存储NSMutableDictionay到你的NSUserDefaults
因此,当您想要取消当时任何[dictionary valueforkey @"KEY"]特定的本地通知时,您将在哪里作为密钥写入
您传递您的ID,以便您获得该特定的本地通知并将其传递给
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Run Code Online (Sandbox Code Playgroud)
斯威夫特5:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)
Run Code Online (Sandbox Code Playgroud)