Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DONT MERGE] Update code iOS 7+8 #488

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 136 additions & 83 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
{
Expand All @@ -49,98 +50,129 @@ - (void)register:(CDVInvokedUrlCommand*)command;

NSMutableDictionary* options = [command.arguments objectAtIndex:0];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone;
#endif
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 __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
self.callback = [options objectForKey:@"ecb"];

if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) {

UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone;

if ([badgeArg isKindOfClass:[NSString class]])
{
if ([badgeArg isEqualToString:@"true"]) {
UserNotificationTypes |= UIUserNotificationTypeBadge;
}
}
else if ([badgeArg boolValue]) {
UserNotificationTypes |= UIUserNotificationTypeBadge;
#endif
}
}
else if ([badgeArg boolValue]) {
notificationTypes |= UIRemoteNotificationTypeBadge;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
UserNotificationTypes |= UIUserNotificationTypeBadge;
#endif
}

if ([soundArg isKindOfClass:[NSString class]])
{
if ([soundArg isEqualToString:@"true"]) {
notificationTypes |= UIRemoteNotificationTypeSound;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([soundArg isKindOfClass:[NSString class]])
{
if ([soundArg isEqualToString:@"true"]) {
UserNotificationTypes |= UIUserNotificationTypeSound;
}
}
else if ([soundArg boolValue]) {
UserNotificationTypes |= UIUserNotificationTypeSound;
#endif
}
}
else if ([soundArg boolValue]) {
notificationTypes |= UIRemoteNotificationTypeSound;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
UserNotificationTypes |= UIUserNotificationTypeSound;
#endif
}
}

if ([alertArg isKindOfClass:[NSString class]])
{
if ([alertArg isEqualToString:@"true"]) {
notificationTypes |= UIRemoteNotificationTypeAlert;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([alertArg isKindOfClass:[NSString class]])
{
if ([alertArg isEqualToString:@"true"]) {
UserNotificationTypes |= UIUserNotificationTypeAlert;
}
}
else if ([alertArg boolValue]) {
UserNotificationTypes |= UIUserNotificationTypeAlert;
#endif
}
}
else if ([alertArg boolValue]) {
notificationTypes |= UIRemoteNotificationTypeAlert;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
UserNotificationTypes |= UIUserNotificationTypeAlert;
#endif
}

notificationTypes |= UIRemoteNotificationTypeNewsstandContentAvailability;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
UserNotificationTypes |= UIUserNotificationActivationModeBackground;
#endif

self.callback = [options objectForKey:@"ecb"];
}

if (notificationTypes == UIRemoteNotificationTypeNone)
NSLog(@"PushPlugin.register: Push notification type is set to none");
UserNotificationTypes |= UIUserNotificationActivationModeBackground;

isInline = NO;
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

UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone;


if ([badgeArg isKindOfClass:[NSString class]])
{
if ([badgeArg isEqualToString:@"true"]) {
notificationTypes |= UIRemoteNotificationTypeBadge;
}
}
else if ([badgeArg boolValue]) {
notificationTypes |= UIRemoteNotificationTypeBadge;
}

if ([soundArg isKindOfClass:[NSString class]])
{
if ([soundArg isEqualToString:@"true"]) {
notificationTypes |= UIRemoteNotificationTypeSound;
}
}
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 <ios8


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];
NSString *jsStatement = [NSString stringWithFormat:@"navigator.PushPlugin.isEnabled = %d;", type != UIRemoteNotificationTypeNone];
NSLog(@"JSStatement %@",jsStatement);

if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) {

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);

}
}
*/


- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

Expand All @@ -155,28 +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 = [[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 (!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
// 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"];
Expand Down