Skip to content

Commit

Permalink
OneSignal notification buttons not displaying fix
Browse files Browse the repository at this point in the history
* Re-listing notifications categories after registering to force a refresh of them.
   - See comment for more details.
* These are the dynamic per notification buttons set via the dashboard or REST API
* Issue started to show up in iOS 12
* Fixes #430
  • Loading branch information
jkasten2 committed Jan 14, 2020
1 parent ac76834 commit 268e88b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -666,26 +666,31 @@ + (void)addActionButtons:(OSNotificationPayload*)payload
var allCategories = OneSignalNotificationCategoryController.sharedInstance.existingCategories;

let newCategoryIdentifier = [OneSignalNotificationCategoryController.sharedInstance registerNotificationCategoryForNotificationId:payload.notificationID];

let category = [UNNotificationCategory categoryWithIdentifier:newCategoryIdentifier
actions:finalActionArray
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];

if (allCategories) {
let newCategorySet = [NSMutableSet new];
for(UNNotificationCategory *existingCategory in allCategories) {
if (![existingCategory.identifier isEqualToString:newCategoryIdentifier])
[newCategorySet addObject:existingCategory];
}

[newCategorySet addObject:category];
allCategories = newCategorySet;
}
else
allCategories = [[NSMutableSet alloc] initWithArray:@[category]];

[UNUserNotificationCenter.currentNotificationCenter setNotificationCategories:allCategories];

[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:allCategories];
// List Categories again so iOS refreshes it's internal list.
// Required otherwise buttons will not display or won't update.
// This is a blackbox assumption, the delay on the main thread this call creates might be giving
// some iOS background thread time to flush to disk.
allCategories = OneSignalNotificationCategoryController.sharedInstance.existingCategories;

content.categoryIdentifier = newCategoryIdentifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ - (NSString *)registerNotificationCategoryForNotificationId:(NSString *)notifica
return categoryId;
}

// Get all existing Notifications Categories in a blocking way
- (NSMutableSet<UNNotificationCategory*>*)existingCategories {
__block NSMutableSet* allCategories;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
Expand Down

0 comments on commit 268e88b

Please sign in to comment.