Skip to content

Commit

Permalink
- Added option to override default in-app display of launch urls
Browse files Browse the repository at this point in the history
- Fixed iOS 7 crash on init
- Fixed handling notifications when app active and in-app alerts are
  false
- Fixed auto-prompt not calling issue
- Fixed Null ActionID when app in focus and InAppAlerts are ON.
- Fixed issue where swizzling would still run even if OneSignal SDK is
  not setup in the project.
  • Loading branch information
JKalash committed Aug 10, 2016
1 parent a4de072 commit 8d7215f
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 23 deletions.
2 changes: 1 addition & 1 deletion OneSignal.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OneSignal"
s.version = "2.0.3"
s.version = "2.0.4"
s.summary = "OneSignal push notification library for mobile apps."
s.homepage = "https://onesignal.com"
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ extern NSString * const kOSSettingsKeyAutoPrompt;
/*Enable the default in-app alerts*/
extern NSString * const kOSSettingsKeyInAppAlerts;

/*Enable In-App display of Launch URLs*/
extern NSString * const kOSSettingsKeyInAppLaunchURL;

/**
`OneSignal` provides a high level interface to interact with OneSignal's push service.
Expand Down
Binary file modified iOS_SDK/Framework/OneSignal.framework/Versions/A/OneSignal
Binary file not shown.
3 changes: 3 additions & 0 deletions iOS_SDK/OneSignal/OneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ extern NSString * const kOSSettingsKeyAutoPrompt;
/*Enable the default in-app alerts*/
extern NSString * const kOSSettingsKeyInAppAlerts;

/*Enable In-App display of Launch URLs*/
extern NSString * const kOSSettingsKeyInAppLaunchURL;

/**
`OneSignal` provides a high level interface to interact with OneSignal's push service.
Expand Down
35 changes: 26 additions & 9 deletions iOS_SDK/OneSignal/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@
/*Enable the default in-app alerts*/
NSString * const kOSSettingsKeyInAppAlerts = @"kOSSettingsKeyInAppAlerts";

/*Enable the default in-app launch urls*/
NSString * const kOSSettingsKeyInAppLaunchURL = @"kOSSettingsKeyInAppLaunchURL";


@implementation OneSignal

NSString* const ONESIGNAL_VERSION = @"020002";
NSString* const ONESIGNAL_VERSION = @"020004";

static bool registeredWithApple = false; //Has attempted to register for push notifications with Apple.
static OneSignalTrackIAP* trackIAPPurchase;
Expand Down Expand Up @@ -163,14 +166,23 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
mNotificationTypes = [self getNotificationTypes];

//Check if in-app setting passed assigned
if([settings[kOSSettingsKeyInAppAlerts] isKindOfClass:[NSNumber class]])
if(settings[kOSSettingsKeyInAppAlerts] && [settings[kOSSettingsKeyInAppAlerts] isKindOfClass:[NSNumber class]])
[self enableInAppAlertNotification:settings[kOSSettingsKeyInAppAlerts]];
else [self enableInAppAlertNotification:@YES];

// Register this device with Apple's APNS server.
BOOL autoPrompt = [settings[kOSSettingsKeyAutoPrompt] isKindOfClass:[NSNumber class]] && [@YES isEqualToNumber:settings[kOSSettingsKeyAutoPrompt]];
//Check if disabled in-app launch url if passed a NO
if(settings[kOSSettingsKeyInAppLaunchURL] && [settings[kOSSettingsKeyInAppLaunchURL] isKindOfClass:[NSNumber class]])
[self enableInAppLaunchURL:settings[kOSSettingsKeyInAppLaunchURL]];
else [self enableInAppLaunchURL:@YES];

// Register this device with Apple's APNS server if enabled auto-prompt or not passed a NO
BOOL autoPrompt = YES;
if(settings[kOSSettingsKeyAutoPrompt] && [settings[kOSSettingsKeyAutoPrompt] isKindOfClass:[NSNumber class]])
autoPrompt = [settings[kOSSettingsKeyAutoPrompt] boolValue];
if (autoPrompt || registeredWithApple)
[self registerForPushNotifications];


// iOS 8 - Register for remote notifications to get a token now since registerUserNotificationSettings is what shows the prompt.
else if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
[[UIApplication sharedApplication] registerForRemoteNotifications];
Expand Down Expand Up @@ -449,6 +461,11 @@ + (void)enableInAppAlertNotification:(NSNumber*)enable {
[[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)enableInAppLaunchURL:(NSNumber*)enable {
[[NSUserDefaults standardUserDefaults] setObject:enable forKey:@"ONESIGNAL_INAPP_LAUNCH_URL"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)setSubscription:(BOOL)enable {
NSString* value = nil;
if (!enable)
Expand Down Expand Up @@ -734,20 +751,18 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive {

inAppAlert = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_ALERT"] boolValue];

[OneSignalHelper lastMessageReceived:messageDict];

if (inAppAlert) {
[OneSignalHelper lastMessageReceived:messageDict];

NSArray<NSString*>* titleAndBody = [OneSignalHelper getPushTitleBody:messageDict];


id oneSignalAlertViewDelegate = [[OneSignalAlertViewDelegate alloc] initWithMessageDict:messageDict];

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:titleAndBody[0] ? titleAndBody[0] : @""
message:titleAndBody[1] ? titleAndBody[1] : @""
delegate:oneSignalAlertViewDelegate
cancelButtonTitle:@"Close"
otherButtonTitles:nil, nil];

//Add Buttons
NSArray *additionalData = [OneSignalHelper getActionButtons];
if (additionalData) {
Expand All @@ -762,6 +777,9 @@ + (void)notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive {

return;
}

//App is active and a notification was received without inApp display. Display type is none
[OneSignalHelper handleNotificationReceived:None];
}
else {

Expand Down Expand Up @@ -821,7 +839,6 @@ + (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc

[self clearBadgeCount:true];


NSString* actionID = NULL;
if (actionType == ActionTaken) {
actionID = messageDict[@"custom"][@"a"][@"actionSelected"];
Expand Down
6 changes: 5 additions & 1 deletion iOS_SDK/OneSignal/OneSignalAlertViewDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)button
NSMutableDictionary* customDict = [userInfo[@"custom"] mutableCopy];
NSMutableDictionary* additionalData = [[NSMutableDictionary alloc] initWithDictionary:customDict[@"a"]];

additionalData[@"actionSelected"] = additionalData[@"actionButtons"][buttonIndex - 1][@"id"];
if([additionalData[@"actionButtons"] isKindOfClass:[NSArray class]])
additionalData[@"actionSelected"] = additionalData[@"actionButtons"][buttonIndex - 1][@"id"];

else if([mMessageDict[@"o"] isKindOfClass:[NSArray class]])
additionalData[@"actionSelected"] = mMessageDict[@"o"][buttonIndex -1][@"i"];

customDict[@"a"] = additionalData;
userInfo[@"custom"] = customDict;
Expand Down
4 changes: 0 additions & 4 deletions iOS_SDK/OneSignal/OneSignalHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ - (id)init {
- (NSMutableURLRequest*) requestWithMethod:(NSString*)method
path:(NSString*)path {

if([method containsString:@"on_focus"] || [method containsString:@"on_session"])
NSLog(@"Calling %@", method);


NSURL* url = [NSURL URLWithString:path relativeToURL:self.baseURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:method];
Expand Down
28 changes: 20 additions & 8 deletions iOS_SDK/OneSignal/OneSignalHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -582,14 +582,15 @@ + (void)enqueueRequest:(NSURLRequest*)request onSuccess:(OSResultSuccessBlock)su
}

+ (void)handleJSONNSURLResponse:(NSURLResponse*) response data:(NSData*) data error:(NSError*) error onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {

NSHTTPURLResponse* HTTPResponse = (NSHTTPURLResponse*)response;
NSInteger statusCode = [HTTPResponse statusCode];
NSError* jsonError;
NSError* jsonError = nil;
NSMutableDictionary* innerJson;

if (data != nil && [data length] > 0) {
innerJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
if (jsonError != nil) {
if (jsonError) {
if (failureBlock != nil)
failureBlock([NSError errorWithDomain:@"OneSignal Error" code:statusCode userInfo:@{@"returned" : jsonError}]);
return;
Expand All @@ -616,14 +617,25 @@ + (void)handleJSONNSURLResponse:(NSURLResponse*) response data:(NSData*) data er

+ (void) displayWebView:(NSURL*)url {

if(!webVC)
webVC = [[OneSignalWebView alloc] init];
webVC.url = url;
[webVC showInApp];
//Check if in-app or safari
BOOL inAppLaunch = YES;
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_LAUNCH_URL"]) {
[[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"ONESIGNAL_INAPP_LAUNCH_URL"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

inAppLaunch = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_LAUNCH_URL"] boolValue];

if(inAppLaunch) {
if(!webVC)
webVC = [[OneSignalWebView alloc] init];
webVC.url = url;
[webVC showInApp];
}
else [[UIApplication sharedApplication] openURL:url];

}



#pragma clang diagnostic pop
#pragma clang diagnostic pop
#pragma clang diagnostic pop
Expand Down
22 changes: 22 additions & 0 deletions iOS_SDK/OneSignal/UIApplication+Swizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ +(Class)delegateClass {
}

- (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)inDeviceToken {

if(![OneSignal app_id]) return;

[OneSignal didRegisterForRemoteNotifications:app deviceToken:inDeviceToken];

if ([self respondsToSelector:@selector(oneSignalDidRegisterForRemoteNotifications:deviceToken:)])
[self oneSignalDidRegisterForRemoteNotifications:app deviceToken:inDeviceToken];
}

- (void)oneSignalDidFailRegisterForRemoteNotification:(UIApplication*)app error:(NSError*)err {

if(![OneSignal app_id]) return;

[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat: @"Error registering for Apple push notifications. Error: %@", err]];

if ([self respondsToSelector:@selector(oneSignalDidFailRegisterForRemoteNotification:error:)])
Expand All @@ -99,6 +105,8 @@ - (void)oneSignalDidFailRegisterForRemoteNotification:(UIApplication*)app error:

- (void)oneSignalDidRegisterUserNotifications:(UIApplication*)application settings:(UIUserNotificationSettings*)notificationSettings {

if(![OneSignal app_id]) return;

[OneSignal updateNotificationTypes:notificationSettings.types];
if ([self respondsToSelector:@selector(oneSignalDidRegisterUserNotifications:settings:)])
[self oneSignalDidRegisterUserNotifications:application settings:notificationSettings];
Expand All @@ -108,6 +116,8 @@ - (void)oneSignalDidRegisterUserNotifications:(UIApplication*)application settin
// Notification opened! iOS 6 ONLY!
- (void)oneSignalReceivedRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo {

if(![OneSignal app_id]) return;

[OneSignal notificationOpened:userInfo isActive:[application applicationState] == UIApplicationStateActive];

if ([self respondsToSelector:@selector(oneSignalReceivedRemoteNotification:userInfo:)])
Expand All @@ -117,6 +127,8 @@ - (void)oneSignalReceivedRemoteNotification:(UIApplication*)application userInfo
// User Tap on Notification while app was in background - OR - Notification received (silent or not, foreground or background) on iOS 7+
- (void) oneSignalRemoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult)) completionHandler {

if(![OneSignal app_id]) return;

//Call notificationAction if app is active -> not a silent notification but rather user tap on notification
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
[OneSignal notificationOpened:userInfo isActive:YES];
Expand All @@ -135,6 +147,8 @@ - (void) oneSignalRemoteSilentNotification:(UIApplication*)application UserInfo:

- (void) oneSignalLocalNotificationOpened:(UIApplication*)application handleActionWithIdentifier:(NSString*)identifier forLocalNotification:(UILocalNotification*)notification completionHandler:(void(^)()) completionHandler {

if(![OneSignal app_id]) return;

[OneSignal processLocalActionBasedNotification:notification identifier:identifier];

if ([self respondsToSelector:@selector(oneSignalLocalNotificationOpened:handleActionWithIdentifier:forLocalNotification:completionHandler:)])
Expand All @@ -144,6 +158,8 @@ - (void) oneSignalLocalNotificationOpened:(UIApplication*)application handleActi

- (void)oneSignalLocalNotificationOpened:(UIApplication*)application notification:(UILocalNotification*)notification {

if(![OneSignal app_id]) return;

[OneSignal processLocalActionBasedNotification:notification identifier:@"__DEFAULT__"];

if([self respondsToSelector:@selector(oneSignalLocalNotificationOpened:notification:)])
Expand All @@ -152,6 +168,8 @@ - (void)oneSignalLocalNotificationOpened:(UIApplication*)application notificatio

- (void)oneSignalApplicationWillResignActive:(UIApplication*)application {

if(![OneSignal app_id]) return;

[OneSignalTracker onFocus:YES];

if ([self respondsToSelector:@selector(oneSignalApplicationWillResignActive:)])
Expand All @@ -160,6 +178,8 @@ - (void)oneSignalApplicationWillResignActive:(UIApplication*)application {

- (void)oneSignalApplicationDidBecomeActive:(UIApplication*)application {

if(![OneSignal app_id]) return;

[OneSignalTracker onFocus:NO];

if ([self respondsToSelector:@selector(oneSignalApplicationDidBecomeActive:)])
Expand All @@ -168,6 +188,8 @@ - (void)oneSignalApplicationDidBecomeActive:(UIApplication*)application {

-(void)oneSignalApplicationWillTerminate:(UIApplication *)application {

if(![OneSignal app_id]) return;

[OneSignalTracker onFocus:YES];

if ([self respondsToSelector:@selector(oneSignalApplicationWillTerminate:)])
Expand Down

0 comments on commit 8d7215f

Please sign in to comment.