我正在使用 UILocalNotification
对象向我的应用程序发出通知。目前,每次生成事件时,我都会弹出一个通知。
notification.alertBody=@"Event Occurs 2";
notification.alertAction=@"Open";
[[UIApplication sharedApplication]presentLocalNotificationNow:notification];
但是,由于事件不断发生,每次都会生成新的通知。
有没有办法更新一个通知,如果它已经存在,如果不存在则创建一个新的通知。
最佳答案
您无法更新已安排的本地通知。但是,您可以取消它并重新安排一个新的。
取消本地通知:
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
if ([uid isEqualToString:uidtodelete])
{
//Cancelling the specific local notification
[app cancelLocalNotification:oneEvent];
//Schedule your new "updated" local notification here.
break;
}
}
这将遍历所有计划的本地通知并删除您要删除的本地通知。请注意,您需要为每个通知设置一个唯一的属性以区分其他通知(在上面的示例中,假定 userInfo 包含一个唯一的“uid”)。
感谢 KingofBliss 在 how to delete specific local notifications 上提供上述代码.
关于ios - 如何覆盖或更新 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23075167/