From b41e96274dcd580d68961132fb5ea5c22bb457db Mon Sep 17 00:00:00 2001 From: Kasten Date: Wed, 2 Nov 2016 22:29:36 -0700 Subject: [PATCH 1/3] Fixed AppDelegate compaiblity and added isAppInFocus --- iOS_SDK/OneSignal/OneSignal.h | 3 ++ iOS_SDK/OneSignal/OneSignal.m | 15 ++++---- iOS_SDK/OneSignal/OneSignalHelper.m | 18 ++++++---- .../UNUserNotificationCenter+OneSignal.m | 34 +++++++++++++++---- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/iOS_SDK/OneSignal/OneSignal.h b/iOS_SDK/OneSignal/OneSignal.h index 99748fdfb..b0b88821d 100755 --- a/iOS_SDK/OneSignal/OneSignal.h +++ b/iOS_SDK/OneSignal/OneSignal.h @@ -148,6 +148,9 @@ typedef OSNotificationDisplayType OSInFocusDisplayOption; Set to false when app is in focus and in-app alerts are disabled, or the remote notification is silent. */ @property(readonly, getter=wasShown)BOOL shown; +/* Set to true if the app was in focus when the notification */ +@property(readonly, getter=wasAppInFocus)BOOL isAppInFocus; + /* Set to true when the received notification is silent Silent means there is no alert, sound, or badge payload in the aps dictionary requires remote-notification within UIBackgroundModes array of the Info.plist */ diff --git a/iOS_SDK/OneSignal/OneSignal.m b/iOS_SDK/OneSignal/OneSignal.m index 230c4a161..0758de233 100755 --- a/iOS_SDK/OneSignal/OneSignal.m +++ b/iOS_SDK/OneSignal/OneSignal.m @@ -1206,14 +1206,13 @@ - (void)oneSignalReceivedRemoteNotification:(UIApplication*)application userInfo // User Tap on Notification while app was in background - OR - Notification received (silent or not, foreground or background) on iOS 7+ - (void) oneSignalRemoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult)) completionHandler { - if([OneSignal app_id]) { - - //Call notificationAction if app is active -> not a silent notification but rather user tap on notification - //Unless iOS 10+ then call remoteSilentNotification instead. - if([UIApplication sharedApplication].applicationState == UIApplicationStateActive && userInfo[@"aps"][@"alert"]) + if ([OneSignal app_id]) { + // Call notificationAction if app is active -> not a silent notification but rather user tap on notification + // Unless iOS 10+ then call remoteSilentNotification instead. + if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && userInfo[@"aps"][@"alert"]) [OneSignal notificationOpened:userInfo isActive:YES]; - else [OneSignal remoteSilentNotification:application UserInfo:userInfo]; - + else + [OneSignal remoteSilentNotification:application UserInfo:userInfo]; } if ([self respondsToSelector:@selector(oneSignalRemoteSilentNotification:UserInfo:fetchCompletionHandler:)]) { @@ -1221,7 +1220,7 @@ - (void) oneSignalRemoteSilentNotification:(UIApplication*)application UserInfo: return; } - //Make sure not a cold start from tap on notification (OS doesn't call didReceiveRemoteNotification) + // Make sure not a cold start from tap on notification (OS doesn't call didReceiveRemoteNotification) if ([self respondsToSelector:@selector(oneSignalReceivedRemoteNotification:userInfo:)] && ![[OneSignal valueForKey:@"coldStartFromTapOnNotification"] boolValue]) [self oneSignalReceivedRemoteNotification:application userInfo:userInfo]; diff --git a/iOS_SDK/OneSignal/OneSignalHelper.m b/iOS_SDK/OneSignal/OneSignalHelper.m index 47d2fb5cc..334fd7ef1 100644 --- a/iOS_SDK/OneSignal/OneSignalHelper.m +++ b/iOS_SDK/OneSignal/OneSignalHelper.m @@ -144,7 +144,7 @@ - (id)initWithRawMessage:(NSDictionary*)message { @end @implementation OSNotification -@synthesize payload = _payload, shown = _shown, silentNotification = _silentNotification, displayType = _displayType; +@synthesize payload = _payload, shown = _shown, isAppInFocus = _isAppInFocus, silentNotification = _silentNotification, displayType = _displayType; #if XC8_AVAILABLE @synthesize mutableContent = _mutableContent; @@ -165,11 +165,12 @@ - (id)initWithPayload:(OSNotificationPayload *)payload displayType:(OSNotificati _shown = true; - BOOL isActive = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive; + _isAppInFocus = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive; //If remote silent -> shown = false //If app is active and in-app alerts are not enabled -> shown = false - if(_silentNotification || (isActive && [[NSUserDefaults standardUserDefaults] boolForKey:@"ONESIGNAL_ALERT_OPTION"] == OSNotificationDisplayTypeNone)) + if (_silentNotification || + (_isAppInFocus && [[NSUserDefaults standardUserDefaults] boolForKey:@"ONESIGNAL_ALERT_OPTION"] == OSNotificationDisplayTypeNone)) _shown = false; } @@ -216,10 +217,11 @@ - (NSString*)stringify { if(self.displayType) [obj setObject:@(self.displayType) forKeyedSubscript: @"displayType"]; - if(self.shown) - [obj setObject:@(self.shown) forKeyedSubscript: @"shown"]; - if(self.silentNotification) + [obj setObject:@(self.shown) forKeyedSubscript: @"shown"]; + [obj setObject:@(self.isAppInFocus) forKeyedSubscript: @"isAppInFocus"]; + + if (self.silentNotification) [obj setObject:@(self.silentNotification) forKeyedSubscript: @"silentNotification"]; @@ -494,7 +496,7 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf if(!NSClassFromString(@"UNNotificationAction") || !NSClassFromString(@"UNNotificationRequest")) return NULL; NSMutableArray * actionArray = [[NSMutableArray alloc] init]; - for( NSDictionary* button in data[@"o"]) { + for(NSDictionary* button in data[@"o"]) { NSString* title = button[@"n"] != NULL ? button[@"n"] : @""; NSString* buttonID = button[@"i"] != NULL ? button[@"i"] : title; id action = [NSClassFromString(@"UNNotificationAction") actionWithIdentifier:buttonID title:title options:UNNotificationActionOptionForeground]; @@ -668,6 +670,8 @@ + (void)enqueueRequest:(NSURLRequest*)request onSuccess:(OSResultSuccessBlock)su + (void)enqueueRequest:(NSURLRequest*)request onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock isSynchronous:(BOOL)isSynchronous { + NSLog(@"HERE!!!!!!!!!"); + NSLog(@"%@", [NSString stringWithFormat:@"request.body: %@", [[NSString alloc]initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]]); [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message: [NSString stringWithFormat:@"request.body: %@", [[NSString alloc]initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]]]; if (isSynchronous) { diff --git a/iOS_SDK/OneSignal/UNUserNotificationCenter+OneSignal.m b/iOS_SDK/OneSignal/UNUserNotificationCenter+OneSignal.m index 776fa1005..28e0f688f 100644 --- a/iOS_SDK/OneSignal/UNUserNotificationCenter+OneSignal.m +++ b/iOS_SDK/OneSignal/UNUserNotificationCenter+OneSignal.m @@ -45,6 +45,13 @@ @interface OneSignal (UN_extra) + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive; @end +// This class hooks into the following iSO 10 UNUserNotificationCenterDelegate selectors: +// - userNotificationCenter:willPresentNotification:withCompletionHandler: +// - Reads kOSSettingsKeyInFocusDisplayOption to respect it's setting. +// - userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: +// - Used to process opening a notifications. +// - The presents of this selector tells iOS to no longer fire `application:didReceiveRemoteNotification:fetchCompletionHandler:`. +// We call this to maintain existing behavior. @implementation sizzleUNUserNotif @@ -119,7 +126,7 @@ - (void)onesignalUserNotificationCenter:(id)center didReceiveNotificationRespons NSDictionary* usrInfo = [[[[response performSelector:@selector(notification)] valueForKey:@"request"] valueForKey:@"content"] valueForKey:@"userInfo"]; if (!usrInfo || [usrInfo count] == 0) { - [sizzleUNUserNotif tunnelToDelegate:center :response :completionHandler]; + [sizzleUNUserNotif tunnelToDelegate:center response:response handler:completionHandler]; return; } @@ -150,7 +157,7 @@ - (void)onesignalUserNotificationCenter:(id)center didReceiveNotificationRespons BOOL isActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive && [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"] intValue] != OSNotificationDisplayTypeNotification; [OneSignal notificationOpened:usrInfo isActive:isActive]; - [sizzleUNUserNotif tunnelToDelegate:center :response :completionHandler]; + [sizzleUNUserNotif tunnelToDelegate:center response:response handler:completionHandler]; return; } @@ -179,18 +186,31 @@ - (void)onesignalUserNotificationCenter:(id)center didReceiveNotificationRespons userInfo[@"aps"] = @{ @"alert" : userInfo[@"m"] }; } - BOOL isActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive && - [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"] intValue] != OSNotificationDisplayTypeNotification; + UIApplication *sharedApp = [UIApplication sharedApplication]; + BOOL isActive = sharedApp.applicationState == UIApplicationStateActive && + [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"] intValue] != OSNotificationDisplayTypeNotification; - [OneSignal notificationOpened:userInfo isActive:isActive]; - [sizzleUNUserNotif tunnelToDelegate:center :response :completionHandler]; + if ([OneSignal app_id]) + [OneSignal notificationOpened:userInfo isActive:isActive]; + [sizzleUNUserNotif tunnelToDelegate:center response:response handler:completionHandler]; + // Call orginal selector if one was set. if ([self respondsToSelector:@selector(onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)]) [self onesignalUserNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler]; + else if ([sharedApp.delegate respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]) { + // Call depercated pre-iOS 10 selector if one as set on the AppDelegate. + // NOTE: Should always be true as our AppDelegate swizzling should be there unless something else unsizzled it. + [sharedApp.delegate application:sharedApp didReceiveRemoteNotification:usrInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) { + // Call iOS 10's compleationHandler from iOS 9's completion handler. + completionHandler(); + }]; + } } -+ (void)tunnelToDelegate:(id)center :(id)response :(void (^)())handler { +// Depercated - [OneSignal notificationCenterDelegate] - Now handled by swizzling. +// Just need to keep the `handler();` call ++ (void)tunnelToDelegate:(id)center response:(id)response handler:(void (^)())handler { if ([[OneSignal notificationCenterDelegate] respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)]) [[OneSignal notificationCenterDelegate] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:handler]; From 94c3ce7de1eef54f6ea93f696f3e1e54e74e7f3c Mon Sep 17 00:00:00 2001 From: Kasten Date: Wed, 2 Nov 2016 22:33:50 -0700 Subject: [PATCH 2/3] Removed some logging --- iOS_SDK/OneSignal/OneSignalHelper.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/iOS_SDK/OneSignal/OneSignalHelper.m b/iOS_SDK/OneSignal/OneSignalHelper.m index 334fd7ef1..7dcbd691e 100644 --- a/iOS_SDK/OneSignal/OneSignalHelper.m +++ b/iOS_SDK/OneSignal/OneSignalHelper.m @@ -669,9 +669,6 @@ + (void)enqueueRequest:(NSURLRequest*)request onSuccess:(OSResultSuccessBlock)su } + (void)enqueueRequest:(NSURLRequest*)request onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock isSynchronous:(BOOL)isSynchronous { - - NSLog(@"HERE!!!!!!!!!"); - NSLog(@"%@", [NSString stringWithFormat:@"request.body: %@", [[NSString alloc]initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]]); [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message: [NSString stringWithFormat:@"request.body: %@", [[NSString alloc]initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]]]; if (isSynchronous) { From b569c781711704ff8deb04690ea11c7c828a38e6 Mon Sep 17 00:00:00 2001 From: Kasten Date: Fri, 4 Nov 2016 17:46:35 -0700 Subject: [PATCH 3/3] nil body and double action event firing fixes * Fixed issue where title was set instead of body on OSNotification. * Fixed case where handleNotificationAction could fire 2 times. - This would happen if you did the following. - 1. Set display inAppFoucs option as None. - 2. Send notification - 3. Background app and then send another. - 4. Tap on 2nd notification. - 5. The block would fire 2 times, once for each notificaiton. --- iOS_SDK/OneSignal/OneSignal.m | 23 ++++--- iOS_SDK/OneSignal/OneSignalHelper.h | 2 +- iOS_SDK/OneSignal/OneSignalHelper.m | 93 ++++++++++++++++------------- 3 files changed, 65 insertions(+), 53 deletions(-) diff --git a/iOS_SDK/OneSignal/OneSignal.m b/iOS_SDK/OneSignal/OneSignal.m index 0758de233..ad8fb1292 100755 --- a/iOS_SDK/OneSignal/OneSignal.m +++ b/iOS_SDK/OneSignal/OneSignal.m @@ -782,6 +782,8 @@ + (void)sendPurchases:(NSArray*)purchases { } + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive { + // Should be called first, other methods relay on this global state below. + [OneSignalHelper lastMessageReceived:messageDict]; NSDictionary* customDict = [messageDict objectForKey:@"os_data"]; if (!customDict) @@ -789,8 +791,7 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive { BOOL inAppAlert = false; if (isActive) { - - if(![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"]) { + if (![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"]) { [[NSUserDefaults standardUserDefaults] setObject:@(OSNotificationDisplayTypeInAppAlert) forKey:@"ONESIGNAL_ALERT_OPTION"]; [[NSUserDefaults standardUserDefaults] synchronize]; } @@ -798,20 +799,18 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive { int iaaoption = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"] intValue]; inAppAlert = iaaoption == OSNotificationDisplayTypeInAppAlert; - [OneSignalHelper lastMessageReceived:messageDict]; - //Make sure it is not a silent one do display, if inAppAlerts are enabled if (inAppAlert && ![OneSignalHelper isRemoteSilentNotification:messageDict]) { - NSArray* titleAndBody = [OneSignalHelper getPushTitleBody:messageDict]; + NSDictionary* titleAndBody = [OneSignalHelper getPushTitleBody:messageDict]; id oneSignalAlertViewDelegate = [[OneSignalAlertViewDelegate alloc] initWithMessageDict:messageDict]; - UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:titleAndBody[0] ? titleAndBody[0] : @"" - message:titleAndBody[1] ? titleAndBody[1] : @"" + UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:titleAndBody[@"title"] + message:titleAndBody[@"body"] delegate:oneSignalAlertViewDelegate cancelButtonTitle:@"Close" otherButtonTitles:nil, nil]; - //Add Buttons + // Add Buttons NSArray *additionalData = [OneSignalHelper getActionButtons]; if (additionalData) { for(id button in additionalData) @@ -820,8 +819,8 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive { [alertView show]; - //Message received that was displayed (Foreground + InAppAlert is true) - //Call Received Block + // Message received that was displayed (Foreground + InAppAlert is true) + // Call Received Block [OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeInAppAlert]; return; @@ -844,12 +843,12 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive { actionSelected = messageDict[@"custom"][@"a"][@"actionSelected"]; type = OSNotificationActionTypeActionTaken; } - if(messageDict[@"actionSelected"]) { + if (messageDict[@"actionSelected"]) { actionSelected = messageDict[@"actionSelected"]; type = OSNotificationActionTypeActionTaken; } - //Call Action Block + // Call Action Block [OneSignalHelper handleNotificationAction:type actionID:actionSelected displayType:OSNotificationDisplayTypeNotification]; [OneSignal handleNotificationOpened:messageDict isActive:isActive actionType:type displayType:OSNotificationDisplayTypeNotification]; } diff --git a/iOS_SDK/OneSignal/OneSignalHelper.h b/iOS_SDK/OneSignal/OneSignalHelper.h index 90b199a8a..0e52f4ef5 100644 --- a/iOS_SDK/OneSignal/OneSignalHelper.h +++ b/iOS_SDK/OneSignal/OneSignalHelper.h @@ -38,7 +38,7 @@ + (void) displayWebView:(NSURL*)url; // - Notification Opened -+ (NSArray*)getPushTitleBody:(NSDictionary*)messageDict; ++ (NSDictionary*)getPushTitleBody:(NSDictionary*)messageDict; + (NSArray*)getActionButtons; + (void)lastMessageReceived:(NSDictionary*)message; + (void)notificationBlocks:(OSHandleNotificationReceivedBlock)receivedBlock :(OSHandleNotificationActionBlock)actionBlock; diff --git a/iOS_SDK/OneSignal/OneSignalHelper.m b/iOS_SDK/OneSignal/OneSignalHelper.m index 7dcbd691e..1ac8b50af 100644 --- a/iOS_SDK/OneSignal/OneSignalHelper.m +++ b/iOS_SDK/OneSignal/OneSignalHelper.m @@ -68,30 +68,32 @@ @implementation OSNotificationPayload - (id)initWithRawMessage:(NSDictionary*)message { self = [super init]; - if(self && message) { - _rawPayload = [NSDictionary dictionaryWithDictionary:message];; + if (self && message) { + _rawPayload = [NSDictionary dictionaryWithDictionary:message]; - if(_rawPayload[@"aps"][@"content-available"]) + if (_rawPayload[@"aps"][@"content-available"]) _contentAvailable = (BOOL)_rawPayload[@"aps"][@"content-available"]; - else _contentAvailable = NO; + else + _contentAvailable = NO; - if(_rawPayload[@"aps"][@"badge"]) + if (_rawPayload[@"aps"][@"badge"]) _badge = (int)_rawPayload[@"aps"][@"badge"]; else _badge = (int)_rawPayload[@"badge"]; _actionButtons = _rawPayload[@"o"]; - if(!_actionButtons) + if (!_actionButtons) _actionButtons = _rawPayload[@"os_data"][@"buttons"][@"o"]; if(_rawPayload[@"aps"][@"sound"]) _sound = _rawPayload[@"aps"][@"sound"]; else if(_rawPayload[@"s"]) _sound = _rawPayload[@"s"]; - else _sound = _rawPayload[@"os_data"][@"buttons"][@"s"]; + else + _sound = _rawPayload[@"os_data"][@"buttons"][@"s"]; if(_rawPayload[@"custom"]) { NSDictionary * custom = _rawPayload[@"custom"]; - if(custom[@"a"]) + if (custom[@"a"]) _additionalData = [custom[@"a"] copy]; _notificationID = custom[@"i"]; _launchURL = custom[@"u"]; @@ -115,22 +117,23 @@ - (id)initWithRawMessage:(NSDictionary*)message { if(_rawPayload[@"m"]) { id m = _rawPayload[@"m"]; - if([m isKindOfClass:[NSDictionary class]]) { + if ([m isKindOfClass:[NSDictionary class]]) { _body = m[@"body"]; _title = m[@"title"]; _subtitle = m[@"subtitle"]; } - //Content-only - else _body = m; + else + _body = m; } else if(_rawPayload[@"aps"][@"alert"]) { id a = message[@"aps"][@"alert"]; - if([a isKindOfClass:[NSDictionary class]]) { + if ([a isKindOfClass:[NSDictionary class]]) { _body = a[@"body"]; _title = a[@"title"]; _subtitle = a[@"subtitle"]; } - else _title = a; + else + _body = a; } else if(_rawPayload[@"os_data"][@"buttons"][@"m"]) { NSDictionary * m = _rawPayload[@"os_data"][@"buttons"][@"m"]; @@ -139,6 +142,7 @@ - (id)initWithRawMessage:(NSDictionary*)message { _subtitle = m[@"subtitle"]; } } + return self; } @end @@ -313,55 +317,61 @@ + (NSArray*)getActionButtons { return lastMessageReceived[@"o"]; } -+ (NSArray*)getPushTitleBody:(NSDictionary*)messageDict { ++ (NSDictionary*)getPushTitleBody:(NSDictionary*)messageDict { NSString *title = messageDict[@"m"][@"title"]; - NSString *body = messageDict[@"m"][@"body"]; - if(!title) { - if([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) + if (!title) { + if ([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) title = messageDict[@"aps"][@"alert"][@"title"]; - else title = messageDict[@"aps"][@"alert"]; + else + title = messageDict[@"aps"][@"alert"]; } - if(!title) + if (!title) title = messageDict[@"os_data"][@"buttons"][@"m"][@"title"]; - if(!title) + if (!title) title = [[[NSBundle mainBundle] infoDictionary] objectForKey:(id)kCFBundleNameKey]; - if(!title) title = @""; + if (!title) + title = @""; - if(!body && [messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) + + NSString *body = messageDict[@"m"][@"body"]; + if (!body && [messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) body = messageDict[@"aps"][@"alert"][@"body"]; - if(!body) + if (!body) body = messageDict[@"os_data"][@"buttons"][@"m"][@"body"]; - if(!body) + if (!body) body = @""; - return @[title, body]; + return @{@"title" : title, @"body": body}; } // Prevent the OSNotification blocks from firing if we receive a Non-OneSignal remote push + (BOOL)isOneSignalPayload { - if(!lastMessageReceived) return NO; + if (!lastMessageReceived) + return NO; return lastMessageReceived[@"custom"][@"i"] || lastMessageReceived[@"os_data"][@"i"]; } + (void)handleNotificationReceived:(OSNotificationDisplayType)displayType { - if (!handleNotificationReceived || ![self isOneSignalPayload]) return; - + if (!handleNotificationReceived || ![self isOneSignalPayload]) + return; OSNotificationPayload *payload = [[OSNotificationPayload alloc] initWithRawMessage:lastMessageReceived]; OSNotification *notification = [[OSNotification alloc] initWithPayload:payload displayType:displayType]; //Prevent duplicate calls to same action static NSString* lastMessageID = @""; - if([payload.notificationID isEqualToString:lastMessageID]) return; + if ([payload.notificationID isEqualToString:lastMessageID]) + return; lastMessageID = payload.notificationID; handleNotificationReceived(notification); } + (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString*)actionID displayType:(OSNotificationDisplayType)displayType { - if (!handleNotificationAction || ![self isOneSignalPayload]) return; + if (!handleNotificationAction || ![self isOneSignalPayload]) + return; OSNotificationAction *action = [[OSNotificationAction alloc] initWithActionType:actionType :actionID]; OSNotificationPayload *payload = [[OSNotificationPayload alloc] initWithRawMessage:lastMessageReceived]; @@ -370,7 +380,8 @@ + (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:( //Prevent duplicate calls to same action static NSString* lastMessageID = @""; - if([payload.notificationID isEqualToString:lastMessageID]) return; + if ([payload.notificationID isEqualToString:lastMessageID]) + return; lastMessageID = payload.notificationID; handleNotificationAction(result); @@ -484,7 +495,8 @@ + (void)requestAuthorization { + (void)registerAsUNNotificationCenterDelegate { Class UNNofiCenterClass = NSClassFromString(@"UNUserNotificationCenter"); - if (!UNNofiCenterClass) return; + if (!UNNofiCenterClass) + return; UNUserNotificationCenter *curNotifCenter = [UNNofiCenterClass currentNotificationCenter]; if (!curNotifCenter.delegate) @@ -493,7 +505,8 @@ + (void)registerAsUNNotificationCenterDelegate { + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInfo { - if(!NSClassFromString(@"UNNotificationAction") || !NSClassFromString(@"UNNotificationRequest")) return NULL; + if (!NSClassFromString(@"UNNotificationAction") || !NSClassFromString(@"UNNotificationRequest")) + return NULL; NSMutableArray * actionArray = [[NSMutableArray alloc] init]; for(NSDictionary* button in data[@"o"]) { @@ -515,7 +528,7 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf id content = [[NSClassFromString(@"UNMutableNotificationContent") alloc] init]; [content setValue:@"__dynamic__" forKey:@"categoryIdentifier"]; - if(data[@"m"]) { + if (data[@"m"]) { if([data[@"m"] isKindOfClass:[NSDictionary class]]) { if(data[@"m"][@"title"]) [content setValue:data[@"m"][@"title"] forKey:@"title"]; @@ -524,26 +537,26 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf if(data[@"m"][@"subtitle"]) [content setValue:data[@"m"][@"subtitle"] forKey:@"subtitle"]; } - else [content setValue:data[@"m"] forKey:@"body"]; + else + [content setValue:data[@"m"] forKey:@"body"]; } else if(data[@"aps"][@"alert"]) { - if([data[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) { + if ([data[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) { [content setValue:data[@"aps"][@"alert"][@"title"] forKey:@"title"]; [content setValue:data[@"aps"][@"alert"][@"body"] forKey:@"body"]; [content setValue:data[@"aps"][@"alert"][@"subtitle"] forKey:@"subtitle"]; } - else [content setValue:data[@"aps"][@"alert"] forKey:@"body"]; + else + [content setValue:data[@"aps"][@"alert"] forKey:@"body"]; } [content setValue:userInfo forKey:@"userInfo"]; - if(data[@"s"]) { - + if (data[@"s"]) { id defaultSound = [NSClassFromString(@"UNNotificationSound") performSelector:@selector(soundNamed:) withObject:data[@"s"]]; [content setValue:defaultSound forKey:@"sound"]; } - else [content setValue:[NSClassFromString(@"UNNotificationSound") performSelector:@selector(defaultSound)] forKey:@"sound"];