Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'delete-multiple' notification type #953

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions NextcloudTalk/NCNotificationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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]];
}
}
}];
Expand All @@ -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<UNNotificationRequest *> * _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<UNNotification *> * _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);
}
}];

Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/NCPushNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef NS_ENUM(NSInteger, NCPushNotificationType) {
NCPushNotificationTypeChat,
NCPushNotificationTypeDelete,
NCPushNotificationTypeDeleteAll,
NCPushNotificationTypeDeleteMultiple,
NCPushNotificationTypeAdminNotification
};

Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions NextcloudTalk/NCPushNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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];

Expand Down