2017-05-04 43 views
2

取消本地单个通知u能帮助我如何取消在iOS的10如何在目标C

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
[center removeAllPendingNotificationRequests]; 
[center removePendingNotificationRequestsWithIdentifiers:@[ CYLInviteCategoryIdentifier ]]; 

removePendingNotificationRequestsWithIdentifiers本地通知我无法理解

+1

可你不明白什么removePendingNotificationRequestsWithIdentifiers?它将删除标识符出现在阵列上的所有待处理通知请求。 –

+0

[Cancel UILocalNotification]的可能重复(http://stackoverflow.com/questions/3158264/cancel-uilocalnotification) – Spartan

回答

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

在创建一个本地通知,你可以传递一个标识符每个通知。使用相同的标识符删除本地通知。

代码来创建本地通知: -

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]; 
0

试试这个

[[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]]; 
     } 
       } 

    }];