diff --git a/NextcloudTalk/NCNotificationController.m b/NextcloudTalk/NCNotificationController.m index 97dfba0f7..ed98ab4d1 100644 --- a/NextcloudTalk/NCNotificationController.m +++ b/NextcloudTalk/NCNotificationController.m @@ -82,11 +82,13 @@ - (void)requestAuthorization - (void)processBackgroundPushNotification:(NCPushNotification *)pushNotification { if (pushNotification) { - NSInteger notificationId = pushNotification.notificationId; if (pushNotification.type == NCPushNotificationTypeDelete) { - [self removeNotificationWithNotificationId:notificationId forAccountId:pushNotification.accountId]; + NSNumber *notificationId = @(pushNotification.notificationId); + [self removeNotificationWithNotificationIds:@[notificationId] forAccountId:pushNotification.accountId]; } else if (pushNotification.type == NCPushNotificationTypeDeleteAll) { [self removeAllNotificationsForAccountId:pushNotification.accountId]; + } else if (pushNotification.type == NCPushNotificationTypeDeleteMultiple) { + [self removeNotificationWithNotificationIds:pushNotification.notificationIds forAccountId:pushNotification.accountId]; } else { NSLog(@"Push Notification of an unknown type received"); } @@ -192,7 +194,7 @@ - (void)removeAllNotificationsForAccountId:(NSString *)accountId for (UNNotificationRequest *notificationRequest in requests) { NSString *notificationAccountId = [notificationRequest.content.userInfo objectForKey:@"accountId"]; if (notificationAccountId && [notificationAccountId isEqualToString:accountId]) { - [self->_notificationCenter removeDeliveredNotificationsWithIdentifiers:@[notificationRequest.identifier]]; + [self->_notificationCenter removePendingNotificationRequestsWithIdentifiers:@[notificationRequest.identifier]]; } } }]; @@ -210,30 +212,43 @@ - (void)removeAllNotificationsForAccountId:(NSString *)accountId [self updateAppIconBadgeNumber]; } -- (void)removeNotificationWithNotificationId:(NSInteger)notificationId forAccountId:(NSString *)accountId +- (void)removeNotificationWithNotificationIds:(NSArray *)notificationIds forAccountId:(NSString *)accountId { + if (!notificationIds) { + return; + } + + void(^removeNotification)(UNNotificationRequest *, BOOL) = ^(UNNotificationRequest *notificationRequest, BOOL isPending) { + NSString *notificationString = [notificationRequest.content.userInfo objectForKey:@"pushNotification"]; + NSString *notificationAccountId = [notificationRequest.content.userInfo objectForKey:@"accountId"]; + NCPushNotification *pushNotification = [NCPushNotification pushNotificationFromDecryptedString:notificationString withAccountId:notificationAccountId]; + + if (!pushNotification || ![pushNotification.accountId isEqualToString:accountId]) { + return; + } + + if ([notificationIds containsObject:@(pushNotification.notificationId)]) { + if (isPending) { + [self->_notificationCenter removePendingNotificationRequestsWithIdentifiers:@[notificationRequest.identifier]]; + } else { + [self->_notificationCenter removeDeliveredNotificationsWithIdentifiers:@[notificationRequest.identifier]]; + } + + [[NCDatabaseManager sharedInstance] decreaseUnreadBadgeNumberForAccountId:accountId]; + } + }; + // Check in pending notifications [_notificationCenter getPendingNotificationRequestsWithCompletionHandler:^(NSArray * _Nonnull requests) { for (UNNotificationRequest *notificationRequest in requests) { - NSString *notificationString = [notificationRequest.content.userInfo objectForKey:@"pushNotification"]; - NSString *notificationAccountId = [notificationRequest.content.userInfo objectForKey:@"accountId"]; - NCPushNotification *pushNotification = [NCPushNotification pushNotificationFromDecryptedString:notificationString withAccountId:notificationAccountId]; - if (pushNotification && [pushNotification.accountId isEqualToString:accountId] && pushNotification.notificationId == notificationId) { - [self->_notificationCenter removeDeliveredNotificationsWithIdentifiers:@[notificationRequest.identifier]]; - [[NCDatabaseManager sharedInstance] decreaseUnreadBadgeNumberForAccountId:accountId]; - } + removeNotification(notificationRequest, YES); } }]; + // Check in delivered notifications [_notificationCenter getDeliveredNotificationsWithCompletionHandler:^(NSArray * _Nonnull notifications) { for (UNNotification *notification in notifications) { - NSString *notificationString = [notification.request.content.userInfo objectForKey:@"pushNotification"]; - NSString *notificationAccountId = [notification.request.content.userInfo objectForKey:@"accountId"]; - NCPushNotification *pushNotification = [NCPushNotification pushNotificationFromDecryptedString:notificationString withAccountId:notificationAccountId]; - if (pushNotification && [pushNotification.accountId isEqualToString:accountId] && pushNotification.notificationId == notificationId) { - [self->_notificationCenter removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]]; - [[NCDatabaseManager sharedInstance] decreaseUnreadBadgeNumberForAccountId:accountId]; - } + removeNotification(notification.request, NO); } }]; diff --git a/NextcloudTalk/NCPushNotification.h b/NextcloudTalk/NCPushNotification.h index a99441343..214e34f5c 100644 --- a/NextcloudTalk/NCPushNotification.h +++ b/NextcloudTalk/NCPushNotification.h @@ -29,6 +29,7 @@ typedef NS_ENUM(NSInteger, NCPushNotificationType) { NCPushNotificationTypeChat, NCPushNotificationTypeDelete, NCPushNotificationTypeDeleteAll, + NCPushNotificationTypeDeleteMultiple, NCPushNotificationTypeAdminNotification }; @@ -53,6 +54,7 @@ extern NSString * const NCPushNotificationJoinVideoCallAcceptedNotification; @property (nonatomic, copy) NSString *roomToken; @property (nonatomic, assign) NSInteger roomId; @property (nonatomic, assign) NSInteger notificationId; +@property (nonatomic, strong) NSArray *notificationIds; @property (nonatomic, copy) NSString *accountId; @property (nonatomic, copy) NSString *jsonString; @property (nonatomic, copy) NSString *responseUserText; diff --git a/NextcloudTalk/NCPushNotification.m b/NextcloudTalk/NCPushNotification.m index 906bfe8ff..1df87ca32 100644 --- a/NextcloudTalk/NCPushNotification.m +++ b/NextcloudTalk/NCPushNotification.m @@ -30,11 +30,13 @@ @implementation NCPushNotification NSString * const kNCPNSubjectKey = @"subject"; NSString * const kNCPNIdKey = @"id"; NSString * const kNCPNNotifIdKey = @"nid"; +NSString * const kNCPNNotifIdsKey = @"nids"; NSString * const kNCPNTypeCallKey = @"call"; NSString * const kNCPNTypeRoomKey = @"room"; NSString * const kNCPNTypeChatKey = @"chat"; NSString * const kNCPNTypeDeleteKey = @"delete"; NSString * const kNCPNTypeDeleteAllKey = @"delete-all"; +NSString * const kNCPNTypeDeleteMultipleKey = @"delete-multiple"; NSString * const kNCPNAppIdAdminNotificationKey = @"admin_notification_talk"; NSString * const NCPushNotificationJoinChatNotification = @"NCPushNotificationJoinChatNotification"; @@ -87,6 +89,9 @@ + (instancetype)nonTalkPushNotification:(id)jsonNotification withAccountId:(NSSt pushNotification.type = NCPushNotificationTypeDelete; } else if ([jsonNotification objectForKey:kNCPNTypeDeleteAllKey]) { pushNotification.type = NCPushNotificationTypeDeleteAll; + } else if ([jsonNotification objectForKey:kNCPNTypeDeleteMultipleKey]) { + pushNotification.notificationIds = [jsonNotification objectForKey:kNCPNNotifIdsKey]; + pushNotification.type = NCPushNotificationTypeDeleteMultiple; } else { NSString *app = [jsonNotification objectForKey:kNCPNAppKey];