我正在为表格中的某些行设置特定日期和时间的本地通知。所以考虑一下
案例1:当用户第一次设置本地通知时,他从日期选择器中选择日期,我将其传递给本地通知对象。
NSArray *notificationarray = [[UIApplication sharedApplication]scheduledLocalNotifications];
if([notificationarray count]== 0)
{
m_alarmLocalNotification = [[UILocalNotification alloc] init];
m_alarmLocalNotification.fireDate = DateTime;
m_alarmLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:m_Name forKey:@"ID"];
m_alarmLocalNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:m_alarmLocalNotification];
案例2:修改本地通知日期。
NSArray *notificationarray = [[UIApplication sharedApplication]scheduledLocalNotifications];
for (int i = 0;i < [notificationarray count];i++)
{
UILocalNotification *notificationObject=[notificationarray objectAtIndex:i];
NSString *Name=[notificationObject.userInfo valueForKey:@"ID"];
if(Name isEqualToString:m_Name])
{
[[UIApplication sharedApplication] cancelLocalNotification:notificationObject];
m_alarmLocalNotification = [[UILocalNotification alloc] init];
m_alarmLocalNotification.fireDate = DateTime;
m_alarmLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:m_noteName forKey:@"ID"];
m_alarmLocalNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:m_alarmLocalNotification];
}
else
{
m_alarmLocalNotification = [[UILocalNotification alloc] init];
m_alarmLocalNotification.fireDate = DateTime;
m_alarmLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:m_noteName forKey:@"ID"];
m_alarmLocalNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:m_alarmLocalNotification];
}
}
案例3:删除本地通知。
NSArray *notificationarray = [[UIApplication sharedApplication]scheduledLocalNotifications];
NSLog(@"notification count:%d",[notificationarray count]);
for (int i = 0;i < [notificationarray count];i++)
{
UILocalNotification *notificationObject=[notificationarray objectAtIndex:i];
NSString *Name=[notificationObject.userInfo valueForKey:@"ID"];
if([Name isEqualToString:m_Name])
{
[[UIApplication sharedApplication] cancelLocalNotification:notificationObject];
}
}
面临的问题。 0)我不确定我是否以正确的方式做事。 1)即使我删除了我的应用程序并重新安装它,默认的schedulednotification数组也不会被释放。我的意思是它包含一些以前的通知。 2)每当我删除我的单元格时,我希望本地通知应该被删除。
此致 兰吉特
答案 0 :(得分:0)
如果您在修改通知时使用以下代码,那么事情会怎样?
NSArray *notificationarray = [[UIApplication sharedApplication] scheduledLocalNotifications];
[notificationarray enumerateObjectsUsingBlock:^(UILocalNotification *notification,NSUInteger idx, BOOL *stop) {
if ([[notification.userInfo valueForKey:@"ID"] isEqual:@""]) {
notification.fireDate = [NSDate date];
}
}];