From ff4ade868488ef0fcb014da652681011cd95d8ea Mon Sep 17 00:00:00 2001 From: Morten Henriksen Date: Thu, 25 Dec 2014 12:13:51 +0100 Subject: [PATCH 1/5] Fix iOS 7 missing badge --- src/ios/PushPlugin.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 7686ae9d..e6bfd629 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -106,7 +106,10 @@ - (void)register:(CDVInvokedUrlCommand*)command; #endif } - notificationTypes |= UIRemoteNotificationTypeNewsstandContentAvailability; + // Issue missing ios7 badge updates + // https://github.com/phonegap-build/PushPlugin/issues/365 + // Fixed when newsstand contant + // notificationTypes |= UIRemoteNotificationTypeNewsstandContentAvailability; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 UserNotificationTypes |= UIUserNotificationActivationModeBackground; #endif From 37252870e4432250c63f435eaaac7b591c6a8fdc Mon Sep 17 00:00:00 2001 From: Morten Henriksen Date: Tue, 27 Jan 2015 16:16:15 +0100 Subject: [PATCH 2/5] runtime system check --- src/ios/PushPlugin.m | 105 ++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index e6bfd629..87dfcc44 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -34,6 +34,7 @@ @implementation PushPlugin @synthesize notificationCallbackId; @synthesize callback; +#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) - (void)unregister:(CDVInvokedUrlCommand*)command; { @@ -49,9 +50,10 @@ - (void)register:(CDVInvokedUrlCommand*)command; NSMutableDictionary* options = [command.arguments objectAtIndex:0]; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone; -#endif + } + UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone; id badgeArg = [options objectForKey:@"badge"]; @@ -62,57 +64,66 @@ - (void)register:(CDVInvokedUrlCommand*)command; { if ([badgeArg isEqualToString:@"true"]) { notificationTypes |= UIRemoteNotificationTypeBadge; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationTypeBadge; -#endif + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationTypeBadge; + } } } else if ([badgeArg boolValue]) { notificationTypes |= UIRemoteNotificationTypeBadge; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationTypeBadge; -#endif + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationTypeBadge; + } + } if ([soundArg isKindOfClass:[NSString class]]) { if ([soundArg isEqualToString:@"true"]) { notificationTypes |= UIRemoteNotificationTypeSound; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationTypeSound; -#endif - } + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationTypeSound; + } + } } else if ([soundArg boolValue]) { notificationTypes |= UIRemoteNotificationTypeSound; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationTypeSound; -#endif + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationTypeSound; + } + } if ([alertArg isKindOfClass:[NSString class]]) { if ([alertArg isEqualToString:@"true"]) { notificationTypes |= UIRemoteNotificationTypeAlert; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationTypeAlert; -#endif - } + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationTypeAlert; + } + + } } else if ([alertArg boolValue]) { notificationTypes |= UIRemoteNotificationTypeAlert; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationTypeAlert; -#endif + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationTypeAlert; + } + } // Issue missing ios7 badge updates // https://github.com/phonegap-build/PushPlugin/issues/365 // Fixed when newsstand contant // notificationTypes |= UIRemoteNotificationTypeNewsstandContentAvailability; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - UserNotificationTypes |= UIUserNotificationActivationModeBackground; -#endif + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + UserNotificationTypes |= UIUserNotificationActivationModeBackground; + } self.callback = [options objectForKey:@"ecb"]; @@ -121,29 +132,37 @@ - (void)register:(CDVInvokedUrlCommand*)command; isInline = NO; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) { - UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UserNotificationTypes categories:nil]; - [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; - [[UIApplication sharedApplication] registerForRemoteNotifications]; - } else { - [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; - } -#else - [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; -#endif + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) { + UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UserNotificationTypes categories:nil]; + [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; + [[UIApplication sharedApplication] registerForRemoteNotifications]; + } else { + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; + } + }else{ + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; + } if (notificationMessage) // if there is a pending startup notification [self notificationReceived]; // go ahead and process it } -/* + - (void)isEnabled:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options { - UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + UIRemoteNotificationType type; + + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; + }else{ + type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + } + NSString *jsStatement = [NSString stringWithFormat:@"navigator.PushPlugin.isEnabled = %d;", type != UIRemoteNotificationTypeNone]; NSLog(@"JSStatement %@",jsStatement); } -*/ + - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { @@ -159,8 +178,12 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"appVersion"]; // Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them. - NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; - + NSUInteger rntypes; + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; + }else{ + rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + } // Set the defaults to disabled unless we find otherwise... NSString *pushBadge = @"disabled"; NSString *pushAlert = @"disabled"; From 3e982a5a73d473f9ce8ba87873e89b0e38745762 Mon Sep 17 00:00:00 2001 From: Morten Henriksen Date: Tue, 27 Jan 2015 16:41:40 +0100 Subject: [PATCH 3/5] split up code depending on features --- src/ios/PushPlugin.m | 133 ++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 64 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 87dfcc44..75cb2d9a 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -50,101 +50,106 @@ - (void)register:(CDVInvokedUrlCommand*)command; NSMutableDictionary* options = [command.arguments objectAtIndex:0]; - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { - UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone; - } - - UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone; - id badgeArg = [options objectForKey:@"badge"]; id soundArg = [options objectForKey:@"sound"]; id alertArg = [options objectForKey:@"alert"]; - if ([badgeArg isKindOfClass:[NSString class]]) - { - if ([badgeArg isEqualToString:@"true"]) { - notificationTypes |= UIRemoteNotificationTypeBadge; - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { - UserNotificationTypes |= UIUserNotificationTypeBadge; + self.callback = [options objectForKey:@"ecb"]; + + if ([UIUserNotificationType class]) { + + UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone; + + if ([badgeArg isKindOfClass:[NSString class]]) + { + if ([badgeArg isEqualToString:@"true"]) { + UserNotificationTypes |= UIUserNotificationTypeBadge; } } - } - else if ([badgeArg boolValue]) { - notificationTypes |= UIRemoteNotificationTypeBadge; - - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + else if ([badgeArg boolValue]) { UserNotificationTypes |= UIUserNotificationTypeBadge; } - } - - if ([soundArg isKindOfClass:[NSString class]]) - { - if ([soundArg isEqualToString:@"true"]) { - notificationTypes |= UIRemoteNotificationTypeSound; - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + if ([soundArg isKindOfClass:[NSString class]]) + { + if ([soundArg isEqualToString:@"true"]) { UserNotificationTypes |= UIUserNotificationTypeSound; } } - } - else if ([soundArg boolValue]) { - notificationTypes |= UIRemoteNotificationTypeSound; - - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + else if ([soundArg boolValue]) { UserNotificationTypes |= UIUserNotificationTypeSound; } - } - - if ([alertArg isKindOfClass:[NSString class]]) - { - if ([alertArg isEqualToString:@"true"]) { - notificationTypes |= UIRemoteNotificationTypeAlert; - - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + if ([alertArg isKindOfClass:[NSString class]]) + { + if ([alertArg isEqualToString:@"true"]) { UserNotificationTypes |= UIUserNotificationTypeAlert; } - } - } - else if ([alertArg boolValue]) { - notificationTypes |= UIRemoteNotificationTypeAlert; - - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { + else if ([alertArg boolValue]) { UserNotificationTypes |= UIUserNotificationTypeAlert; } - } + UserNotificationTypes |= UIUserNotificationActivationModeBackground; - // Issue missing ios7 badge updates - // https://github.com/phonegap-build/PushPlugin/issues/365 - // Fixed when newsstand contant - // notificationTypes |= UIRemoteNotificationTypeNewsstandContentAvailability; + isInline = NO; - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { - UserNotificationTypes |= UIUserNotificationActivationModeBackground; - } + UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UserNotificationTypes categories:nil]; + [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; + [[UIApplication sharedApplication] registerForRemoteNotifications]; - self.callback = [options objectForKey:@"ecb"]; + } else { - if (notificationTypes == UIRemoteNotificationTypeNone) - NSLog(@"PushPlugin.register: Push notification type is set to none"); + UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone; - isInline = NO; + if ([badgeArg isKindOfClass:[NSString class]]) + { + if ([badgeArg isEqualToString:@"true"]) { + notificationTypes |= UIRemoteNotificationTypeBadge; + } + } + else if ([badgeArg boolValue]) { + notificationTypes |= UIRemoteNotificationTypeBadge; + } - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { - if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) { - UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UserNotificationTypes categories:nil]; - [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; - [[UIApplication sharedApplication] registerForRemoteNotifications]; - } else { - [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; + if ([soundArg isKindOfClass:[NSString class]]) + { + if ([soundArg isEqualToString:@"true"]) { + notificationTypes |= UIRemoteNotificationTypeSound; + } } - }else{ + else if ([soundArg boolValue]) { + notificationTypes |= UIRemoteNotificationTypeSound; + } + + + if ([alertArg isKindOfClass:[NSString class]]) + { + if ([alertArg isEqualToString:@"true"]) { + notificationTypes |= UIRemoteNotificationTypeAlert; + } + } + else if ([alertArg boolValue]) { + notificationTypes |= UIRemoteNotificationTypeAlert; + } + + + // Issue missing ios7 badge updates + // https://github.com/phonegap-build/PushPlugin/issues/365 + // Fixed when newsstand contant + // notificationTypes |= UIRemoteNotificationTypeNewsstandContentAvailability; + + if (notificationTypes == UIRemoteNotificationTypeNone) + NSLog(@"PushPlugin.register: Push notification type is set to none"); + + isInline = NO; + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; - } + } // EO Date: Tue, 27 Jan 2015 17:00:39 +0100 Subject: [PATCH 4/5] damn --- src/ios/PushPlugin.m | 76 ++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 75cb2d9a..576a28fe 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -156,16 +156,21 @@ - (void)register:(CDVInvokedUrlCommand*)command; - (void)isEnabled:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options { - UIRemoteNotificationType type; - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { - type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; - }else{ - type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; - } + if ([UIUserNotificationType class]) { + + UIUserNotificationType type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; + + NSString *jsStatement = [NSString stringWithFormat:@"navigator.PushPlugin.isEnabled = %d;", type != UIUserNotificationTypeNone]; + NSLog(@"JSStatement %@",jsStatement); + } else { + + UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; - NSString *jsStatement = [NSString stringWithFormat:@"navigator.PushPlugin.isEnabled = %d;", type != UIRemoteNotificationTypeNone]; - NSLog(@"JSStatement %@",jsStatement); + NSString *jsStatement = [NSString stringWithFormat:@"navigator.PushPlugin.isEnabled = %d;", type != UIRemoteNotificationTypeNone]; + NSLog(@"JSStatement %@",jsStatement); + + } } @@ -182,32 +187,49 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] forKey:@"appName"]; [results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"appVersion"]; - // Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them. - NSUInteger rntypes; - if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { - rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; - }else{ - rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; - } // Set the defaults to disabled unless we find otherwise... NSString *pushBadge = @"disabled"; NSString *pushAlert = @"disabled"; NSString *pushSound = @"disabled"; - // Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which - // one is actually disabled. So we are literally checking to see if rnTypes matches what is turned on, instead of by number. The "tricky" part is that the - // single notification types will only match if they are the ONLY one enabled. Likewise, when we are checking for a pair of notifications, it will only be - // true if those two notifications are on. This is why the code is written this way - if(rntypes & UIRemoteNotificationTypeBadge){ - pushBadge = @"enabled"; - } - if(rntypes & UIRemoteNotificationTypeAlert) { - pushAlert = @"enabled"; - } - if(rntypes & UIRemoteNotificationTypeSound) { - pushSound = @"enabled"; + + // Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them. + if ([UIUserNotificationType class]) { + NSUInteger rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; + + // Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which + // one is actually disabled. So we are literally checking to see if rnTypes matches what is turned on, instead of by number. The "tricky" part is that the + // single notification types will only match if they are the ONLY one enabled. Likewise, when we are checking for a pair of notifications, it will only be + // true if those two notifications are on. This is why the code is written this way + if(rntypes & UIUserNotificationTypeBadge){ + pushBadge = @"enabled"; + } + if(rntypes & UIUserNotificationTypeAlert) { + pushAlert = @"enabled"; + } + if(rntypes & UIUserNotificationTypeSound) { + pushSound = @"enabled"; + } + + } else { + NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + + // Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which + // one is actually disabled. So we are literally checking to see if rnTypes matches what is turned on, instead of by number. The "tricky" part is that the + // single notification types will only match if they are the ONLY one enabled. Likewise, when we are checking for a pair of notifications, it will only be + // true if those two notifications are on. This is why the code is written this way + if(rntypes & UIRemoteNotificationTypeBadge){ + pushBadge = @"enabled"; + } + if(rntypes & UIRemoteNotificationTypeAlert) { + pushAlert = @"enabled"; + } + if(rntypes & UIRemoteNotificationTypeSound) { + pushSound = @"enabled"; + } } + [results setValue:pushBadge forKey:@"pushBadge"]; [results setValue:pushAlert forKey:@"pushAlert"]; [results setValue:pushSound forKey:@"pushSound"]; From c4e3aa69c66bde45472e81ac303a9e39020c9cc7 Mon Sep 17 00:00:00 2001 From: Morten Henriksen Date: Tue, 27 Jan 2015 17:17:55 +0100 Subject: [PATCH 5/5] switch to basic version test --- src/ios/PushPlugin.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 576a28fe..aab6ed5c 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -56,7 +56,7 @@ - (void)register:(CDVInvokedUrlCommand*)command; self.callback = [options objectForKey:@"ecb"]; - if ([UIUserNotificationType class]) { + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone; @@ -157,7 +157,7 @@ - (void)register:(CDVInvokedUrlCommand*)command; - (void)isEnabled:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options { - if ([UIUserNotificationType class]) { + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { UIUserNotificationType type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; @@ -194,7 +194,7 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them. - if ([UIUserNotificationType class]) { + if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) { NSUInteger rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; // Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which