你可以帮我解决如何取消iOS 10中的本地通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllPendingNotificationRequests];
[center removePendingNotificationRequestsWithIdentifiers:@[ CYLInviteCategoryIdentifier ]];
removePendingNotificationRequestsWithIdentifiers
我无法理解
答案 0 :(得分:3)
创建本地通知时,您可以将标识符传递给每个通知。使用相同的标识符删除本地通知。
创建本地通知的代码: -
NSString *identifier = @"Unique Identifier";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
}];
取消通知的代码: -
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
NSArray *array = [NSArray arrayWithObjects:@"Identifier1",@"Identifier2", nil];
[center removePendingNotificationRequestsWithIdentifiers:array];
答案 1 :(得分:1)
试试这个
[[UNUserNotificationCenter currentNotificationCenter]getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
NSLog(@"count%lu",(unsigned long)requests.count);
if (requests.count>0) {
UNNotificationRequest *pendingRequest = [requests objectAtIndex:0];
if ([pendingRequest.identifier isEqualToString:@"identifier"]) {
[[UNUserNotificationCenter currentNotificationCenter]removePendingNotificationRequestsWithIdentifiers:@[pendingRequest.identifier]];
}
}
}];
答案 2 :(得分:0)
您可以使用以下代码删除单个本地通知。
UIApplication *app = [UIApplication sharedApplication];
NSArray *allLocalNoti = [app scheduledLocalNotifications];
for (int i=0; i<[allLocalNoti count]; i++)
{
UILocalNotification* currentLocalNotification = [allLocalNoti objectAtIndex:i];
NSDictionary *userInfoCurrent = currentLocalNotification.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
if ([uid isEqualToString:uidtodelete])
{
[app cancelLocalNotification:currentLocalNotification];
break;
}
}
快乐编码......
答案 3 :(得分:0)
我不使用上述任何解决方案。我不会取消任何单个通知。相反,无论何时用户运行该应用程序,我都会取消所有本地通知,并运行使用新参数重新创建所有本地通知的代码,这将取消所有需要取消的本地通知。更少的代码表面,更少的测试,创建通知通常是一种廉价的操作。许多很多应用程序都使用此解决方案,请考虑您是否可以自己使用此解决方案。