2017-05-04 45 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]]; 
     } 
       } 

    }];