Skip to content

Commit 01620f6

Browse files
committed
iOS media attachments now download on their own thread
* Fixes issues where the UI can hang if the notification is received while the app is in focus.
1 parent d0241ba commit 01620f6

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

iOS_SDK/OneSignal/OneSignal.m

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,14 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
199199
BOOL inAppAlertsPassed = IAASetting && (IAASetting.integerValue == 0 || IAASetting.integerValue == 1);
200200

201201
NSNumber *IFDSetting = settings[kOSSettingsKeyInFocusDisplayOption];
202-
BOOL inFocusDisplayPassed = IFDSetting && IFDSetting.integerValue >-1 && IFDSetting.integerValue < 3;
202+
BOOL inFocusDisplayPassed = IFDSetting && IFDSetting.integerValue > -1 && IFDSetting.integerValue < 3;
203203

204-
if(!inAppAlertsPassed && !inFocusDisplayPassed)
204+
if (!inAppAlertsPassed && !inFocusDisplayPassed)
205205
[self setNotificationDisplayOptions:@(OSNotificationDisplayTypeInAppAlert)];
206-
207-
else if(!inFocusDisplayPassed)
206+
else if (!inFocusDisplayPassed)
208207
[self setNotificationDisplayOptions:IAASetting];
209-
210-
else [self setNotificationDisplayOptions:IFDSetting];
208+
else
209+
[self setNotificationDisplayOptions:IFDSetting];
211210

212211

213212
if (mUserId != nil)
@@ -1030,7 +1029,7 @@ + (void)didRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSDat
10301029

10311030
}
10321031

1033-
+ (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo {
1032+
+ (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
10341033
// If 'm' present then the notification has action buttons attached to it.
10351034
NSDictionary* data = nil;
10361035

@@ -1042,13 +1041,9 @@ + (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
10421041
//Otherwise if titles or body or attachment -> data is everything
10431042
if (data) {
10441043
if (NSClassFromString(@"UNUserNotificationCenter")) {
1045-
if([[OneSignalHelper class] respondsToSelector:NSSelectorFromString(@"addnotificationRequest::")]) {
1046-
SEL selector = NSSelectorFromString(@"addnotificationRequest::");
1047-
typedef void(*func)(id, SEL, NSDictionary*, NSDictionary*);
1048-
func methodToCall;
1049-
methodToCall = (func)[[OneSignalHelper class] methodForSelector:selector];
1050-
methodToCall([OneSignalHelper class], selector, data, userInfo);
1051-
}
1044+
#if XC8_AVAILABLE
1045+
[OneSignalHelper addnotificationRequest:data userInfo:userInfo completionHandler:completionHandler];
1046+
#endif
10521047
}
10531048
else {
10541049
UILocalNotification* notification = [OneSignalHelper prepareUILocalNotification:data :userInfo];

iOS_SDK/OneSignal/OneSignalHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
+ (void) requestAuthorization;
5454
+ (void)clearCachedMedia;
5555
+ (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInfo;
56+
+ (void)addnotificationRequest:(NSDictionary *)data userInfo:(NSDictionary *)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
5657
#endif
5758

5859
// - Notifications

iOS_SDK/OneSignal/OneSignalHelper.m

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,22 @@ - (NSString*)stringify {
275275

276276
@implementation OneSignalHelper
277277

278+
UIBackgroundTaskIdentifier mediaBackgroundTask;
279+
280+
+ (void) beginBackgroundMediaTask {
281+
mediaBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
282+
[OneSignalHelper endBackgroundMediaTask];
283+
}];
284+
}
285+
286+
+ (void) endBackgroundMediaTask {
287+
[[UIApplication sharedApplication] endBackgroundTask: mediaBackgroundTask];
288+
mediaBackgroundTask = UIBackgroundTaskInvalid;
289+
}
290+
291+
292+
293+
278294
OneSignalWebView *webVC;
279295
NSDictionary* lastMessageReceived;
280296
OSHandleNotificationReceivedBlock handleNotificationReceived;
@@ -635,11 +651,12 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf
635651
if ([self verifyURL:URI]) {
636652
/* Synchroneously download file and chache it */
637653
NSString* name = [OneSignalHelper downloadMediaAndSaveInBundle:URI];
638-
if (!name) continue;
654+
if (!name)
655+
continue;
639656
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
640-
NSString*filePath = [paths[0] stringByAppendingPathComponent:name];
641-
NSURL * url = [NSURL fileURLWithPath:filePath];
642-
NSError * error;
657+
NSString* filePath = [paths[0] stringByAppendingPathComponent:name];
658+
NSURL* url = [NSURL fileURLWithPath:filePath];
659+
NSError* error;
643660
id attachment = [NSClassFromString(@"UNNotificationAttachment") attachmentWithIdentifier:key URL:url options:0 error:&error];
644661
if (attachment)
645662
[attachments addObject:attachment];
@@ -669,26 +686,35 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf
669686
return [NSClassFromString(@"UNNotificationRequest") requestWithIdentifier:[self randomStringWithLength:16] content:content trigger:trigger];
670687
}
671688

672-
+ (void)addnotificationRequest:(NSDictionary *)data :(NSDictionary *)userInfo {
673-
if(!NSClassFromString(@"UNUserNotificationCenter")) return;
689+
+ (void)addnotificationRequest:(NSDictionary *)data userInfo:(NSDictionary *)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
690+
if (!NSClassFromString(@"UNUserNotificationCenter"))
691+
return;
674692

675-
id notificationRequest = [OneSignalHelper prepareUNNotificationRequest:data :userInfo];
676-
[[NSClassFromString(@"UNUserNotificationCenter") currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {}];
693+
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
694+
[OneSignalHelper beginBackgroundMediaTask];
695+
id notificationRequest = [OneSignalHelper prepareUNNotificationRequest:data :userInfo];
696+
[[NSClassFromString(@"UNUserNotificationCenter") currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {}];
697+
completionHandler(UIBackgroundFetchResultNewData);
698+
[OneSignalHelper endBackgroundMediaTask];
699+
});
700+
677701
}
678702

679-
//Synchroneously downloads a media
680-
//On success returns bundle resource name, otherwise returns nil
703+
// Synchroneously downloads a media
704+
// On success returns bundle resource name, otherwise returns nil
681705
+(NSString*) downloadMediaAndSaveInBundle:(NSString*) url {
682706

683707
NSArray<NSString*>* supportedExtensions = @[@"aiff", @"wav", @"mp3", @"mp4", @"jpg", @"jpeg", @"png", @"gif", @"mpeg", @"mpg", @"avi", @"m4a", @"m4v"];
684708
NSArray* components = [url componentsSeparatedByString:@"."];
685709

686-
//URL is not to a file
687-
if ([components count] < 2) return NULL;
688-
NSString * extension = [components lastObject];
710+
// URL is not to a file
711+
if ([components count] < 2)
712+
return NULL;
713+
NSString* extension = [components lastObject];
689714

690-
//Unrecognized extention
691-
if(![supportedExtensions containsObject:extension]) return NULL;
715+
// Unrecognized extention
716+
if (![supportedExtensions containsObject:extension])
717+
return NULL;
692718

693719
NSURL * URL = [NSURL URLWithString:url];
694720
NSData * data = [NSData dataWithContentsOfURL:URL];
@@ -703,18 +729,17 @@ +(NSString*) downloadMediaAndSaveInBundle:(NSString*) url {
703729
appendedCache = [[NSMutableArray alloc] initWithArray:cachedFiles];
704730
[appendedCache addObject:name];
705731
}
706-
else appendedCache = [[NSMutableArray alloc] initWithObjects:name, nil];
732+
else
733+
appendedCache = [[NSMutableArray alloc] initWithObjects:name, nil];
707734

708735
[[NSUserDefaults standardUserDefaults] setObject:appendedCache forKey:@"CACHED_MEDIA"];
709736
[[NSUserDefaults standardUserDefaults] synchronize];
710737
return name;
711738
}
712739

713740
+(void)clearCachedMedia {
714-
715-
NSArray * cachedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"CACHED_MEDIA"];
716-
if(cachedFiles) {
717-
741+
NSArray* cachedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"CACHED_MEDIA"];
742+
if (cachedFiles) {
718743
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
719744
for (NSString* file in cachedFiles) {
720745
NSString* filePath = [paths[0] stringByAppendingPathComponent:file];

iOS_SDK/OneSignal/UIApplicationDelegate+OneSignal.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ + (void) handleDidFailRegisterForRemoteNotification:(NSError*)error;
3939
+ (void) updateNotificationTypes:(int)notificationTypes;
4040
+ (NSString*) app_id;
4141
+ (void) notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive;
42-
+ (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo;
42+
+ (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
4343
+ (void) processLocalActionBasedNotification:(UILocalNotification*) notification identifier:(NSString*)identifier;
4444
+ (void) onesignal_Log:(ONE_S_LOG_LEVEL)logLevel message:(NSString*) message;
4545
@end
@@ -189,7 +189,7 @@ - (void) oneSignalRemoteSilentNotification:(UIApplication*)application UserInfo:
189189
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && userInfo[@"aps"][@"alert"])
190190
[OneSignal notificationOpened:userInfo isActive:YES];
191191
else
192-
[OneSignal remoteSilentNotification:application UserInfo:userInfo];
192+
[OneSignal remoteSilentNotification:application UserInfo:userInfo completionHandler:completionHandler];
193193
}
194194

195195
if ([self respondsToSelector:@selector(oneSignalRemoteSilentNotification:UserInfo:fetchCompletionHandler:)]) {

0 commit comments

Comments
 (0)