我似乎找不到它,而且我不确定如何使用 Google 搜索它。在我的应用程序中,我使用后台提取来进行检查,如果有新数据,我会使用 UILocalNotification
向用户发送通知。现在,如果已经有一个通知,我该如何防止应用程序向用户发送新通知?当我几个小时不看手机时,我现在收到了 5 条相同的通知。
谢谢!
最佳答案
您可以使用 UIApplication 的属性 scheduledLocalNotifications
示例代码如下:
NSMutableArray *notifications = [[NSMutableArray alloc] init]; [notifications addObject:notification]; myApp.scheduledLocalNotifications = notifications;
//Equivalent: [myApp setScheduledLocalNotifications:notifications];
UIApplication *myApp = [UIApplication sharedApplication];
NSArray *eventArray = [myApp 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 local notification
[myApp cancelLocalNotification:oneEvent];
break;
}
}
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
if ([localNotification.alertBody isEqualToString:savedTitle]) {
NSLog(@"the notification this is canceld is %@", localNotification.alertBody);
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
希望这有助于找出解决方案。
关于ios - 阻止应用程序发送通知(如果已经存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802917/