Skip to content

Commit

Permalink
updated code on handling configuration of notification categories
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikuB committed Oct 22, 2024
1 parent 466086c commit 21eb952
Showing 1 changed file with 59 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,78 +241,71 @@ - (void)pendingNotificationRequests:(FlutterResult _Nonnull)result

/// Extracts notification categories from [arguments] and configures them as
/// appropriate.
///
/// This code will simply return the `completionHandler` if not running on a
/// compatible iOS version or when no categories were specified in [arguments].
- (void)configureNotificationCategories:(NSDictionary *_Nonnull)arguments
withCompletionHandler:(void (^)(void))completionHandler {
if (@available(iOS 10.0, *)) {
withCompletionHandler:(void (^)(void))completionHandler NS_AVAILABLE_IOS(10.0) {
if ([self containsKey:@"notificationCategories" forDictionary:arguments]) {
NSMutableSet<UNNotificationCategory *> *notificationCategories =
[NSMutableSet set];

NSArray *categories = arguments[@"notificationCategories"];
NSMutableArray<NSString *> *foregroundActionIdentifiers =
[[NSMutableArray alloc] init];

for (NSDictionary *category in categories) {
NSMutableArray<UNNotificationAction *> *newActions =
NSMutableSet<UNNotificationCategory *> *notificationCategories =
[NSMutableSet set];
NSArray *categories = arguments[@"notificationCategories"];
NSMutableArray<NSString *> *foregroundActionIdentifiers =
[[NSMutableArray alloc] init];
for (NSDictionary *category in categories) {
NSMutableArray<UNNotificationAction *> *newActions =
[NSMutableArray array];

NSArray *actions = category[@"actions"];
for (NSDictionary *action in actions) {
NSString *type = action[@"type"];
NSString *identifier = action[@"identifier"];
NSString *title = action[@"title"];
UNNotificationActionOptions options =
[Converters parseNotificationActionOptions:action[@"options"]];

if ((options & UNNotificationActionOptionForeground) != 0) {
[foregroundActionIdentifiers addObject:identifier];
}

if ([type isEqualToString:@"plain"]) {
[newActions
addObject:[UNNotificationAction actionWithIdentifier:identifier
title:title
options:options]];
} else if ([type isEqualToString:@"text"]) {
NSString *buttonTitle = action[@"buttonTitle"];
NSString *placeholder = action[@"placeholder"];
[newActions addObject:[UNTextInputNotificationAction
actionWithIdentifier:identifier
title:title
options:options
textInputButtonTitle:buttonTitle
textInputPlaceholder:placeholder]];
}

NSArray *actions = category[@"actions"];
for (NSDictionary *action in actions) {
NSString *type = action[@"type"];
NSString *identifier = action[@"identifier"];
NSString *title = action[@"title"];
UNNotificationActionOptions options =
[Converters parseNotificationActionOptions:action[@"options"]];

if ((options & UNNotificationActionOptionForeground) != 0) {
[foregroundActionIdentifiers addObject:identifier];
}

if ([type isEqualToString:@"plain"]) {
[newActions
addObject:[UNNotificationAction actionWithIdentifier:identifier
title:title
options:options]];
} else if ([type isEqualToString:@"text"]) {
NSString *buttonTitle = action[@"buttonTitle"];
NSString *placeholder = action[@"placeholder"];
[newActions addObject:[UNTextInputNotificationAction
actionWithIdentifier:identifier
title:title
options:options
textInputButtonTitle:buttonTitle
textInputPlaceholder:placeholder]];
}
}

UNNotificationCategory *notificationCategory = [UNNotificationCategory
categoryWithIdentifier:category[@"identifier"]
actions:newActions
intentIdentifiers:@[]
options:[Converters parseNotificationCategoryOptions:
category[@"options"]]];

[notificationCategories addObject:notificationCategory];
}

UNNotificationCategory *notificationCategory = [UNNotificationCategory
categoryWithIdentifier:category[@"identifier"]
actions:newActions
intentIdentifiers:@[]
options:[Converters parseNotificationCategoryOptions:
category[@"options"]]];

[notificationCategories addObject:notificationCategory];
}

if (notificationCategories.count > 0) {
UNUserNotificationCenter *center =

if (notificationCategories.count > 0) {
UNUserNotificationCenter *center =
[UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:notificationCategories];
[[NSUserDefaults standardUserDefaults]
setObject:foregroundActionIdentifiers
forKey:FOREGROUND_ACTION_IDENTIFIERS];
completionHandler();
} else {
completionHandler();
}
[center setNotificationCategories:notificationCategories];
[[NSUserDefaults standardUserDefaults]
setObject:foregroundActionIdentifiers
forKey:FOREGROUND_ACTION_IDENTIFIERS];
completionHandler();
} else {
completionHandler();
}
}
} else {
completionHandler();
}
}

- (void)getActiveNotifications:(FlutterResult _Nonnull)result
Expand Down

0 comments on commit 21eb952

Please sign in to comment.