Skip to content

Commit 54fd7b4

Browse files
authored
Merge pull request #122 from OneSignal/misc_fixes
Misc fixes
2 parents 9ca39af + 234897f commit 54fd7b4

File tree

3 files changed

+65
-53
lines changed

3 files changed

+65
-53
lines changed

iOS_SDK/OneSignal/OneSignal.m

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -782,36 +782,35 @@ + (void)sendPurchases:(NSArray*)purchases {
782782
}
783783

784784
+ (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive {
785+
// Should be called first, other methods relay on this global state below.
786+
[OneSignalHelper lastMessageReceived:messageDict];
785787

786788
NSDictionary* customDict = [messageDict objectForKey:@"os_data"];
787789
if (!customDict)
788790
customDict = [messageDict objectForKey:@"custom"];
789791

790792
BOOL inAppAlert = false;
791793
if (isActive) {
792-
793-
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"]) {
794+
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"]) {
794795
[[NSUserDefaults standardUserDefaults] setObject:@(OSNotificationDisplayTypeInAppAlert) forKey:@"ONESIGNAL_ALERT_OPTION"];
795796
[[NSUserDefaults standardUserDefaults] synchronize];
796797
}
797798

798799
int iaaoption = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_ALERT_OPTION"] intValue];
799800
inAppAlert = iaaoption == OSNotificationDisplayTypeInAppAlert;
800801

801-
[OneSignalHelper lastMessageReceived:messageDict];
802-
803802
//Make sure it is not a silent one do display, if inAppAlerts are enabled
804803
if (inAppAlert && ![OneSignalHelper isRemoteSilentNotification:messageDict]) {
805804

806-
NSArray<NSString*>* titleAndBody = [OneSignalHelper getPushTitleBody:messageDict];
805+
NSDictionary* titleAndBody = [OneSignalHelper getPushTitleBody:messageDict];
807806
id oneSignalAlertViewDelegate = [[OneSignalAlertViewDelegate alloc] initWithMessageDict:messageDict];
808807

809-
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:titleAndBody[0] ? titleAndBody[0] : @""
810-
message:titleAndBody[1] ? titleAndBody[1] : @""
808+
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:titleAndBody[@"title"]
809+
message:titleAndBody[@"body"]
811810
delegate:oneSignalAlertViewDelegate
812811
cancelButtonTitle:@"Close"
813812
otherButtonTitles:nil, nil];
814-
//Add Buttons
813+
// Add Buttons
815814
NSArray *additionalData = [OneSignalHelper getActionButtons];
816815
if (additionalData) {
817816
for(id button in additionalData)
@@ -820,8 +819,8 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive {
820819

821820
[alertView show];
822821

823-
//Message received that was displayed (Foreground + InAppAlert is true)
824-
//Call Received Block
822+
// Message received that was displayed (Foreground + InAppAlert is true)
823+
// Call Received Block
825824
[OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeInAppAlert];
826825

827826
return;
@@ -844,12 +843,12 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive {
844843
actionSelected = messageDict[@"custom"][@"a"][@"actionSelected"];
845844
type = OSNotificationActionTypeActionTaken;
846845
}
847-
if(messageDict[@"actionSelected"]) {
846+
if (messageDict[@"actionSelected"]) {
848847
actionSelected = messageDict[@"actionSelected"];
849848
type = OSNotificationActionTypeActionTaken;
850849
}
851850

852-
//Call Action Block
851+
// Call Action Block
853852
[OneSignalHelper handleNotificationAction:type actionID:actionSelected displayType:OSNotificationDisplayTypeNotification];
854853
[OneSignal handleNotificationOpened:messageDict isActive:isActive actionType:type displayType:OSNotificationDisplayTypeNotification];
855854
}

iOS_SDK/OneSignal/OneSignalHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
+ (void) displayWebView:(NSURL*)url;
3939

4040
// - Notification Opened
41-
+ (NSArray<NSString*>*)getPushTitleBody:(NSDictionary*)messageDict;
41+
+ (NSDictionary*)getPushTitleBody:(NSDictionary*)messageDict;
4242
+ (NSArray*)getActionButtons;
4343
+ (void)lastMessageReceived:(NSDictionary*)message;
4444
+ (void)notificationBlocks:(OSHandleNotificationReceivedBlock)receivedBlock :(OSHandleNotificationActionBlock)actionBlock;

iOS_SDK/OneSignal/OneSignalHelper.m

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,32 @@ @implementation OSNotificationPayload
6868

6969
- (id)initWithRawMessage:(NSDictionary*)message {
7070
self = [super init];
71-
if(self && message) {
72-
_rawPayload = [NSDictionary dictionaryWithDictionary:message];;
71+
if (self && message) {
72+
_rawPayload = [NSDictionary dictionaryWithDictionary:message];
7373

74-
if(_rawPayload[@"aps"][@"content-available"])
74+
if (_rawPayload[@"aps"][@"content-available"])
7575
_contentAvailable = (BOOL)_rawPayload[@"aps"][@"content-available"];
76-
else _contentAvailable = NO;
76+
else
77+
_contentAvailable = NO;
7778

78-
if(_rawPayload[@"aps"][@"badge"])
79+
if (_rawPayload[@"aps"][@"badge"])
7980
_badge = (int)_rawPayload[@"aps"][@"badge"];
8081
else _badge = (int)_rawPayload[@"badge"];
8182

8283
_actionButtons = _rawPayload[@"o"];
83-
if(!_actionButtons)
84+
if (!_actionButtons)
8485
_actionButtons = _rawPayload[@"os_data"][@"buttons"][@"o"];
8586

8687
if(_rawPayload[@"aps"][@"sound"])
8788
_sound = _rawPayload[@"aps"][@"sound"];
8889
else if(_rawPayload[@"s"])
8990
_sound = _rawPayload[@"s"];
90-
else _sound = _rawPayload[@"os_data"][@"buttons"][@"s"];
91+
else
92+
_sound = _rawPayload[@"os_data"][@"buttons"][@"s"];
9193

9294
if(_rawPayload[@"custom"]) {
9395
NSDictionary * custom = _rawPayload[@"custom"];
94-
if(custom[@"a"])
96+
if (custom[@"a"])
9597
_additionalData = [custom[@"a"] copy];
9698
_notificationID = custom[@"i"];
9799
_launchURL = custom[@"u"];
@@ -115,22 +117,23 @@ - (id)initWithRawMessage:(NSDictionary*)message {
115117

116118
if(_rawPayload[@"m"]) {
117119
id m = _rawPayload[@"m"];
118-
if([m isKindOfClass:[NSDictionary class]]) {
120+
if ([m isKindOfClass:[NSDictionary class]]) {
119121
_body = m[@"body"];
120122
_title = m[@"title"];
121123
_subtitle = m[@"subtitle"];
122124
}
123-
//Content-only
124-
else _body = m;
125+
else
126+
_body = m;
125127
}
126128
else if(_rawPayload[@"aps"][@"alert"]) {
127129
id a = message[@"aps"][@"alert"];
128-
if([a isKindOfClass:[NSDictionary class]]) {
130+
if ([a isKindOfClass:[NSDictionary class]]) {
129131
_body = a[@"body"];
130132
_title = a[@"title"];
131133
_subtitle = a[@"subtitle"];
132134
}
133-
else _title = a;
135+
else
136+
_body = a;
134137
}
135138
else if(_rawPayload[@"os_data"][@"buttons"][@"m"]) {
136139
NSDictionary * m = _rawPayload[@"os_data"][@"buttons"][@"m"];
@@ -139,6 +142,7 @@ - (id)initWithRawMessage:(NSDictionary*)message {
139142
_subtitle = m[@"subtitle"];
140143
}
141144
}
145+
142146
return self;
143147
}
144148
@end
@@ -313,55 +317,61 @@ + (NSArray*)getActionButtons {
313317
return lastMessageReceived[@"o"];
314318
}
315319

316-
+ (NSArray<NSString*>*)getPushTitleBody:(NSDictionary*)messageDict {
320+
+ (NSDictionary*)getPushTitleBody:(NSDictionary*)messageDict {
317321

318322
NSString *title = messageDict[@"m"][@"title"];
319-
NSString *body = messageDict[@"m"][@"body"];
320-
if(!title) {
321-
if([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
323+
if (!title) {
324+
if ([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
322325
title = messageDict[@"aps"][@"alert"][@"title"];
323-
else title = messageDict[@"aps"][@"alert"];
326+
else
327+
title = messageDict[@"aps"][@"alert"];
324328
}
325329

326-
if(!title)
330+
if (!title)
327331
title = messageDict[@"os_data"][@"buttons"][@"m"][@"title"];
328-
if(!title)
332+
if (!title)
329333
title = [[[NSBundle mainBundle] infoDictionary] objectForKey:(id)kCFBundleNameKey];
330-
if(!title) title = @"";
334+
if (!title)
335+
title = @"";
331336

332-
if(!body && [messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
337+
338+
NSString *body = messageDict[@"m"][@"body"];
339+
if (!body && [messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
333340
body = messageDict[@"aps"][@"alert"][@"body"];
334-
if(!body)
341+
if (!body)
335342
body = messageDict[@"os_data"][@"buttons"][@"m"][@"body"];
336-
if(!body)
343+
if (!body)
337344
body = @"";
338345

339-
return @[title, body];
346+
return @{@"title" : title, @"body": body};
340347
}
341348

342349
// Prevent the OSNotification blocks from firing if we receive a Non-OneSignal remote push
343350
+ (BOOL)isOneSignalPayload {
344-
if(!lastMessageReceived) return NO;
351+
if (!lastMessageReceived)
352+
return NO;
345353
return lastMessageReceived[@"custom"][@"i"] || lastMessageReceived[@"os_data"][@"i"];
346354
}
347355

348356
+ (void)handleNotificationReceived:(OSNotificationDisplayType)displayType {
349-
if (!handleNotificationReceived || ![self isOneSignalPayload]) return;
350-
357+
if (!handleNotificationReceived || ![self isOneSignalPayload])
358+
return;
351359

352360
OSNotificationPayload *payload = [[OSNotificationPayload alloc] initWithRawMessage:lastMessageReceived];
353361
OSNotification *notification = [[OSNotification alloc] initWithPayload:payload displayType:displayType];
354362

355363
//Prevent duplicate calls to same action
356364
static NSString* lastMessageID = @"";
357-
if([payload.notificationID isEqualToString:lastMessageID]) return;
365+
if ([payload.notificationID isEqualToString:lastMessageID])
366+
return;
358367
lastMessageID = payload.notificationID;
359368

360369
handleNotificationReceived(notification);
361370
}
362371

363372
+ (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString*)actionID displayType:(OSNotificationDisplayType)displayType {
364-
if (!handleNotificationAction || ![self isOneSignalPayload]) return;
373+
if (!handleNotificationAction || ![self isOneSignalPayload])
374+
return;
365375

366376
OSNotificationAction *action = [[OSNotificationAction alloc] initWithActionType:actionType :actionID];
367377
OSNotificationPayload *payload = [[OSNotificationPayload alloc] initWithRawMessage:lastMessageReceived];
@@ -370,7 +380,8 @@ + (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(
370380

371381
//Prevent duplicate calls to same action
372382
static NSString* lastMessageID = @"";
373-
if([payload.notificationID isEqualToString:lastMessageID]) return;
383+
if ([payload.notificationID isEqualToString:lastMessageID])
384+
return;
374385
lastMessageID = payload.notificationID;
375386

376387
handleNotificationAction(result);
@@ -484,7 +495,8 @@ + (void)requestAuthorization {
484495

485496
+ (void)registerAsUNNotificationCenterDelegate {
486497
Class UNNofiCenterClass = NSClassFromString(@"UNUserNotificationCenter");
487-
if (!UNNofiCenterClass) return;
498+
if (!UNNofiCenterClass)
499+
return;
488500

489501
UNUserNotificationCenter *curNotifCenter = [UNNofiCenterClass currentNotificationCenter];
490502
if (!curNotifCenter.delegate)
@@ -493,7 +505,8 @@ + (void)registerAsUNNotificationCenterDelegate {
493505

494506
+ (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInfo {
495507

496-
if(!NSClassFromString(@"UNNotificationAction") || !NSClassFromString(@"UNNotificationRequest")) return NULL;
508+
if (!NSClassFromString(@"UNNotificationAction") || !NSClassFromString(@"UNNotificationRequest"))
509+
return NULL;
497510

498511
NSMutableArray * actionArray = [[NSMutableArray alloc] init];
499512
for(NSDictionary* button in data[@"o"]) {
@@ -515,7 +528,7 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf
515528
id content = [[NSClassFromString(@"UNMutableNotificationContent") alloc] init];
516529
[content setValue:@"__dynamic__" forKey:@"categoryIdentifier"];
517530

518-
if(data[@"m"]) {
531+
if (data[@"m"]) {
519532
if([data[@"m"] isKindOfClass:[NSDictionary class]]) {
520533
if(data[@"m"][@"title"])
521534
[content setValue:data[@"m"][@"title"] forKey:@"title"];
@@ -524,26 +537,26 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf
524537
if(data[@"m"][@"subtitle"])
525538
[content setValue:data[@"m"][@"subtitle"] forKey:@"subtitle"];
526539
}
527-
else [content setValue:data[@"m"] forKey:@"body"];
540+
else
541+
[content setValue:data[@"m"] forKey:@"body"];
528542
}
529543

530544
else if(data[@"aps"][@"alert"]) {
531-
if([data[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) {
545+
if ([data[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) {
532546
[content setValue:data[@"aps"][@"alert"][@"title"] forKey:@"title"];
533547
[content setValue:data[@"aps"][@"alert"][@"body"] forKey:@"body"];
534548
[content setValue:data[@"aps"][@"alert"][@"subtitle"] forKey:@"subtitle"];
535549
}
536-
else [content setValue:data[@"aps"][@"alert"] forKey:@"body"];
550+
else
551+
[content setValue:data[@"aps"][@"alert"] forKey:@"body"];
537552
}
538553

539554
[content setValue:userInfo forKey:@"userInfo"];
540555

541-
if(data[@"s"]) {
542-
556+
if (data[@"s"]) {
543557
id defaultSound = [NSClassFromString(@"UNNotificationSound") performSelector:@selector(soundNamed:) withObject:data[@"s"]];
544558
[content setValue:defaultSound forKey:@"sound"];
545559
}
546-
547560
else
548561
[content setValue:[NSClassFromString(@"UNNotificationSound") performSelector:@selector(defaultSound)] forKey:@"sound"];
549562

0 commit comments

Comments
 (0)