@@ -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+
278294OneSignalWebView *webVC;
279295NSDictionary * lastMessageReceived;
280296OSHandleNotificationReceivedBlock 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];
0 commit comments