I'm using a UILocalNotification
object to give notification to my application. Currently each time an event is generated i popup a notification.
我使用UILocalNotification对象来通知我的应用程序。目前,每次生成事件时,我都会弹出一个通知。
notification.alertBody=@"Event Occurs 2";
notification.alertAction=@"Open";
[[UIApplication sharedApplication]presentLocalNotificationNow:notification];
But, since the events keep on happening, each time a new notification is generated.
但是,由于事件一直在发生,每次都会生成一个新的通知。
Is there a way to update a notification, if it is already present and create a new notification if not present.
如果通知已经存在,是否有更新通知的方法?如果不存在,是否有创建新通知的方法?
3 个解决方案
#1
4
You can't update an already scheduled Local Notication. You can however, cancel it and reschedule a new one.
您不能更新已经安排好的本地通知。但是你可以取消它,重新安排一个新的。
Cancel your local 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;
}
}
This will loop through all scheduled local notifications and delete the local notification you want deleted. Note, you'll need to set a unique property to each notification to distinguish between others (in the example above, it is assumed userInfo contains a unique "uid").
这将循环遍历所有计划的本地通知,并删除希望删除的本地通知。注意,您需要为每个通知设置一个惟一属性,以区分其他通知(在上面的示例中,假定userInfo包含一个惟一的“uid”)。
Thanks to KingofBliss for code above on how to delete specific local notifications.
感谢KingofBliss,上面关于如何删除特定本地通知的代码。
#2
1
It's not possible to update the notification but if you attach a dictionary with a key to the alert:
不可能更新通知,但如果您在字典中附加一个键到警告:
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:alarmID forKey:@"AlarmKey"];
// Set some extra info to your alarm
notification.userInfo = userInfo;
Then you can retrieve the local notification, cancel it and make a new one with updated content.
然后,您可以检索本地通知,取消它并使用更新的内容创建一个新的通知。
+ (UILocalNotification *)existingNotificationWithAlarmID:(NSString *)alarmID
{
for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[notification.userInfo objectForKey:@"AlarmKey"] isEqualToString:alarmID]) {
return notification;
}
}
return nil;
}
You cancel the notification like this:
您取消通知如下:
- (void)cleanUpLocalNotificationWithAlarmID:(NSString *)alarmID
{
UILocalNotification *notification = [self existingNotificationWithAlarmID:alarmID];
if (notification) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
#3
0
No, there is no way to modify a local notification that has been scheduled. You'll have to cancel the notification and schedule it again.
不,没有办法修改已安排的本地通知。你必须取消通知,重新安排。
#1
4
You can't update an already scheduled Local Notication. You can however, cancel it and reschedule a new one.
您不能更新已经安排好的本地通知。但是你可以取消它,重新安排一个新的。
Cancel your local 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;
}
}
This will loop through all scheduled local notifications and delete the local notification you want deleted. Note, you'll need to set a unique property to each notification to distinguish between others (in the example above, it is assumed userInfo contains a unique "uid").
这将循环遍历所有计划的本地通知,并删除希望删除的本地通知。注意,您需要为每个通知设置一个惟一属性,以区分其他通知(在上面的示例中,假定userInfo包含一个惟一的“uid”)。
Thanks to KingofBliss for code above on how to delete specific local notifications.
感谢KingofBliss,上面关于如何删除特定本地通知的代码。
#2
1
It's not possible to update the notification but if you attach a dictionary with a key to the alert:
不可能更新通知,但如果您在字典中附加一个键到警告:
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:alarmID forKey:@"AlarmKey"];
// Set some extra info to your alarm
notification.userInfo = userInfo;
Then you can retrieve the local notification, cancel it and make a new one with updated content.
然后,您可以检索本地通知,取消它并使用更新的内容创建一个新的通知。
+ (UILocalNotification *)existingNotificationWithAlarmID:(NSString *)alarmID
{
for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[notification.userInfo objectForKey:@"AlarmKey"] isEqualToString:alarmID]) {
return notification;
}
}
return nil;
}
You cancel the notification like this:
您取消通知如下:
- (void)cleanUpLocalNotificationWithAlarmID:(NSString *)alarmID
{
UILocalNotification *notification = [self existingNotificationWithAlarmID:alarmID];
if (notification) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
#3
0
No, there is no way to modify a local notification that has been scheduled. You'll have to cancel the notification and schedule it again.
不,没有办法修改已安排的本地通知。你必须取消通知,重新安排。