Skip to content

Commit 33b434e

Browse files
authored
Merge pull request #119 from OneSignal/all_appdelegate_compat
iOS fixes
2 parents e3992c4 + a76b372 commit 33b434e

7 files changed

+201
-106
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.0.7",
2+
"version": "2.0.8",
33
"name": "onesignal-cordova-plugin",
44
"cordova_name": "OneSignal Push Notifications",
55
"description": "OneSignal is a high volume Push Notification service for mobile apps. In addition to basic notification delivery, OneSignal also provides tools to localize, target, schedule, and automate notifications that you send.",

plugin.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="onesignal-cordova-plugin"
5-
version="2.0.7">
5+
version="2.0.8">
66

77

88
<name>OneSignal Push Notifications</name>
@@ -80,6 +80,13 @@
8080
</array>
8181
</config-file>
8282

83+
<config-file target="*-Debug.plist" parent="aps-environment">
84+
<string>development</string>
85+
</config-file>
86+
<config-file target="*-Release.plist" parent="aps-environment">
87+
<string>production</string>
88+
</config-file>
89+
8390
<!-- Headers -->
8491
<header-file src="src/ios/OneSignal.h" />
8592
<header-file src="src/ios/OneSignalHTTPClient.h" />

src/ios/OneSignal.m

+12-11
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
@implementation OneSignal
7979

80-
NSString* const ONESIGNAL_VERSION = @"020201";
80+
NSString* const ONESIGNAL_VERSION = @"020202";
8181
static NSString* mSDKType = @"native";
8282
static BOOL coldStartFromTapOnNotification = NO;
8383
static BOOL registeredWithApple = NO; //Has attempted to register for push notifications with Apple.
@@ -811,10 +811,10 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive {
811811
cancelButtonTitle:@"Close"
812812
otherButtonTitles:nil, nil];
813813
// Add Buttons
814-
NSArray *additionalData = [OneSignalHelper getActionButtons];
815-
if (additionalData) {
816-
for(id button in additionalData)
817-
[alertView addButtonWithTitle:button[@"n"]];
814+
NSArray *actionButons = [OneSignalHelper getActionButtons:messageDict];
815+
if (actionButons) {
816+
for(id button in actionButons)
817+
[alertView addButtonWithTitle:button[@"n"]];
818818
}
819819

820820
[alertView show];
@@ -1016,8 +1016,7 @@ + (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
10161016
//If buttons -> Data is buttons
10171017
//Otherwise if titles or body or attachment -> data is everything
10181018
if (data) {
1019-
1020-
if(NSClassFromString(@"UNUserNotificationCenter")) {
1019+
if (NSClassFromString(@"UNUserNotificationCenter")) {
10211020
if([[OneSignalHelper class] respondsToSelector:NSSelectorFromString(@"addnotificationRequest::")]) {
10221021
SEL selector = NSSelectorFromString(@"addnotificationRequest::");
10231022
typedef void(*func)(id, SEL, NSDictionary*, NSDictionary*);
@@ -1042,9 +1041,10 @@ + (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
10421041

10431042
/* Handle the notification reception*/
10441043
[OneSignalHelper lastMessageReceived:userInfo];
1045-
if([OneSignalHelper isRemoteSilentNotification:userInfo])
1044+
if ([OneSignalHelper isRemoteSilentNotification:userInfo])
10461045
[OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeNone];
1047-
else [OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeNotification];
1046+
else
1047+
[OneSignalHelper handleNotificationReceived:OSNotificationDisplayTypeNotification];
10481048
}
10491049

10501050
+ (void)processLocalActionBasedNotification:(UILocalNotification*) notification identifier:(NSString*)identifier {
@@ -1062,7 +1062,8 @@ + (void)processLocalActionBasedNotification:(UILocalNotification*) notification
10621062
additionalData = [[NSMutableDictionary alloc] initWithDictionary:customDict[@"a"]];
10631063
optionsDict = userInfo[@"o"];
10641064
}
1065-
else return;
1065+
else
1066+
return;
10661067

10671068
NSMutableArray* buttonArray = [[NSMutableArray alloc] init];
10681069
for (NSDictionary* button in optionsDict) {
@@ -1303,7 +1304,7 @@ + (void)load {
13031304
return;
13041305

13051306
injectToProperClass(@selector(setOneSignalUNDelegate:),
1306-
@selector(setDelegate:), @[], [sizzleUNUserNotif class], UNUserNotificationCenterClass);
1307+
@selector(setDelegate:), @[], [swizzleUNUserNotif class], UNUserNotificationCenterClass);
13071308

13081309
[OneSignalHelper registerAsUNNotificationCenterDelegate];
13091310
#endif

src/ios/OneSignalHelper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
// - Notification Opened
4141
+ (NSDictionary*)getPushTitleBody:(NSDictionary*)messageDict;
42-
+ (NSArray*)getActionButtons;
42+
+ (NSArray*)getActionButtons:(NSDictionary*)messageDict;
4343
+ (void)lastMessageReceived:(NSDictionary*)message;
4444
+ (void)notificationBlocks:(OSHandleNotificationReceivedBlock)receivedBlock :(OSHandleNotificationActionBlock)actionBlock;
4545
+ (void)handleNotificationReceived:(OSNotificationDisplayType)displayType;

src/ios/OneSignalHelper.m

+67-46
Original file line numberDiff line numberDiff line change
@@ -306,44 +306,82 @@ + (NSString*)md5:(NSString *)text {
306306
return NULL;
307307
}
308308

309-
+ (NSArray*)getActionButtons {
309+
+ (NSArray*)getActionButtons:(NSDictionary*)messageDict {
310+
if (messageDict[@"os_data"] && [messageDict[@"os_data"] isKindOfClass:[NSDictionary class]])
311+
return messageDict[@"os_data"][@"buttons"][@"o"];
310312

311-
if(!lastMessageReceived) return NULL;
312-
313-
if(lastMessageReceived[@"os_data"] && [lastMessageReceived[@"os_data"] isKindOfClass:[NSDictionary class]]) {
314-
return lastMessageReceived[@"os_data"][@"buttons"][@"o"];
315-
}
316-
317-
return lastMessageReceived[@"o"];
313+
return messageDict[@"o"];
318314
}
319315

320316
+ (NSDictionary*)getPushTitleBody:(NSDictionary*)messageDict {
321317

322-
NSString *title = messageDict[@"m"][@"title"];
318+
NSString *title;
319+
if ([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
320+
title = messageDict[@"aps"][@"alert"][@"title"];
321+
else
322+
title = messageDict[@"aps"][@"alert"];
323+
323324
if (!title) {
324-
if ([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
325-
title = messageDict[@"aps"][@"alert"][@"title"];
326-
else
327-
title = messageDict[@"aps"][@"alert"];
325+
if ([messageDict[@"m"] isKindOfClass:[NSDictionary class]])
326+
title = messageDict[@"m"][@"title"];
327+
}
328+
329+
if (!title) {
330+
if ([messageDict[@"os_data"][@"buttons"][@"m"] isKindOfClass:[NSDictionary class]])
331+
title = messageDict[@"os_data"][@"buttons"][@"m"][@"title"];
328332
}
329333

330-
if (!title)
331-
title = messageDict[@"os_data"][@"buttons"][@"m"][@"title"];
332334
if (!title)
333335
title = [[[NSBundle mainBundle] infoDictionary] objectForKey:(id)kCFBundleNameKey];
334336
if (!title)
335337
title = @"";
336338

337339

338-
NSString *body = messageDict[@"m"][@"body"];
339-
if (!body && [messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
340+
NSString *subtitle;
341+
if ([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
342+
subtitle = messageDict[@"aps"][@"alert"][@"subtitle"];
343+
else
344+
subtitle = messageDict[@"aps"][@"alert"];
345+
346+
if (!subtitle) {
347+
if ([messageDict[@"m"] isKindOfClass:[NSDictionary class]])
348+
subtitle = messageDict[@"m"][@"subtitle"];
349+
}
350+
351+
if (!subtitle) {
352+
if ([messageDict[@"os_data"][@"buttons"][@"m"] isKindOfClass:[NSDictionary class]])
353+
subtitle = messageDict[@"os_data"][@"buttons"][@"m"][@"subtitle"];
354+
}
355+
356+
if (!subtitle)
357+
subtitle = @"";
358+
359+
360+
361+
NSString *body;
362+
if ([messageDict[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
340363
body = messageDict[@"aps"][@"alert"][@"body"];
341-
if (!body)
342-
body = messageDict[@"os_data"][@"buttons"][@"m"][@"body"];
364+
else
365+
body = messageDict[@"aps"][@"alert"];
366+
367+
if (!body) {
368+
if ([messageDict[@"m"] isKindOfClass:[NSDictionary class]])
369+
body = messageDict[@"m"][@"body"];
370+
else
371+
body = messageDict[@"m"];
372+
}
373+
374+
if (!body) {
375+
if ([messageDict[@"os_data"][@"buttons"][@"m"] isKindOfClass:[NSDictionary class]])
376+
body = messageDict[@"os_data"][@"buttons"][@"m"][@"body"];
377+
else
378+
body = messageDict[@"os_data"][@"buttons"][@"m"];
379+
}
380+
343381
if (!body)
344382
body = @"";
345383

346-
return @{@"title" : title, @"body": body};
384+
return @{@"title" : title, @"subtitle": subtitle, @"body": body};
347385
}
348386

349387
// Prevent the OSNotification blocks from firing if we receive a Non-OneSignal remote push
@@ -408,7 +446,7 @@ + (UILocalNotification*)createUILocalNotification:(NSDictionary*)data {
408446

409447
Class UIMutableUserNotificationActionClass = NSClassFromString(@"UIMutableUserNotificationAction");
410448
NSMutableArray* actionArray = [[NSMutableArray alloc] init];
411-
for (NSDictionary* button in data[@"o"]) {
449+
for (NSDictionary* button in [OneSignalHelper getActionButtons:data]) {
412450
id action = [[UIMutableUserNotificationActionClass alloc] init];
413451
[action setTitle:button[@"n"]];
414452
[action setIdentifier:button[@"i"] ? button[@"i"] : [action title]];
@@ -428,9 +466,10 @@ + (UILocalNotification*)createUILocalNotification:(NSDictionary*)data {
428466
NSUInteger notificationTypes = NOTIFICATION_TYPE_ALL;
429467

430468
NSSet* currentCategories = [[[UIApplication sharedApplication] currentUserNotificationSettings] categories];
431-
if(currentCategories)
469+
if (currentCategories)
432470
currentCategories = [currentCategories setByAddingObject:category];
433-
else currentCategories = [NSSet setWithObject:category];
471+
else
472+
currentCategories = [NSSet setWithObject:category];
434473

435474
[[UIApplication sharedApplication] registerUserNotificationSettings:[uiUserNotificationSettings settingsForTypes:notificationTypes categories:currentCategories]];
436475
notification.category = [category identifier];
@@ -509,7 +548,7 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf
509548
return NULL;
510549

511550
NSMutableArray * actionArray = [[NSMutableArray alloc] init];
512-
for(NSDictionary* button in data[@"o"]) {
551+
for(NSDictionary* button in [OneSignalHelper getActionButtons:data]) {
513552
NSString* title = button[@"n"] != NULL ? button[@"n"] : @"";
514553
NSString* buttonID = button[@"i"] != NULL ? button[@"i"] : title;
515554
id action = [NSClassFromString(@"UNNotificationAction") actionWithIdentifier:buttonID title:title options:UNNotificationActionOptionForeground];
@@ -528,28 +567,10 @@ + (id)prepareUNNotificationRequest:(NSDictionary *)data :(NSDictionary *)userInf
528567
id content = [[NSClassFromString(@"UNMutableNotificationContent") alloc] init];
529568
[content setValue:@"__dynamic__" forKey:@"categoryIdentifier"];
530569

531-
if (data[@"m"]) {
532-
if([data[@"m"] isKindOfClass:[NSDictionary class]]) {
533-
if(data[@"m"][@"title"])
534-
[content setValue:data[@"m"][@"title"] forKey:@"title"];
535-
if(data[@"m"][@"body"])
536-
[content setValue:data[@"m"][@"body"] forKey:@"body"];
537-
if(data[@"m"][@"subtitle"])
538-
[content setValue:data[@"m"][@"subtitle"] forKey:@"subtitle"];
539-
}
540-
else
541-
[content setValue:data[@"m"] forKey:@"body"];
542-
}
543-
544-
else if(data[@"aps"][@"alert"]) {
545-
if ([data[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) {
546-
[content setValue:data[@"aps"][@"alert"][@"title"] forKey:@"title"];
547-
[content setValue:data[@"aps"][@"alert"][@"body"] forKey:@"body"];
548-
[content setValue:data[@"aps"][@"alert"][@"subtitle"] forKey:@"subtitle"];
549-
}
550-
else
551-
[content setValue:data[@"aps"][@"alert"] forKey:@"body"];
552-
}
570+
NSDictionary* alertDict = [OneSignalHelper getPushTitleBody:data];
571+
[content setValue:alertDict[@"title"] forKey:@"title"];
572+
[content setValue:alertDict[@"subtitle"] forKey:@"subtitle"];
573+
[content setValue:alertDict[@"body"] forKey:@"body"];
553574

554575
[content setValue:userInfo forKey:@"userInfo"];
555576

src/ios/UNUserNotificationCenter+OneSignal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#import "OneSignal.h"
3232

3333
#if XC8_AVAILABLE
34-
@interface sizzleUNUserNotif : NSObject
34+
@interface swizzleUNUserNotif : NSObject
3535
@end
3636
#endif
3737

0 commit comments

Comments
 (0)