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"];