From 268e88b2dad7cb3afefbe4bbe2ea91defb9ac137 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Mon, 13 Jan 2020 23:15:54 -0800 Subject: [PATCH] OneSignal notification buttons not displaying fix * 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 --- iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m | 13 +++++++++---- .../OneSignalNotificationCategoryController.m | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m b/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m index 090cd1458..189b80c40 100644 --- a/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m +++ b/iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m @@ -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; } diff --git a/iOS_SDK/OneSignalSDK/Source/OneSignalNotificationCategoryController.m b/iOS_SDK/OneSignalSDK/Source/OneSignalNotificationCategoryController.m index a886437c8..0a21296fc 100644 --- a/iOS_SDK/OneSignalSDK/Source/OneSignalNotificationCategoryController.m +++ b/iOS_SDK/OneSignalSDK/Source/OneSignalNotificationCategoryController.m @@ -98,6 +98,7 @@ - (NSString *)registerNotificationCategoryForNotificationId:(NSString *)notifica return categoryId; } +// Get all existing Notifications Categories in a blocking way - (NSMutableSet*)existingCategories { __block NSMutableSet* allCategories; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);