Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
CA97E14F2051C0A5003B8CB8 /* OneSignalWebOpenDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = CA97E14D2051C0A5003B8CB8 /* OneSignalWebOpenDialog.m */; };
CA97E1502051C0A5003B8CB8 /* OneSignalWebOpenDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = CA97E14D2051C0A5003B8CB8 /* OneSignalWebOpenDialog.m */; };
CA97E1512051C0A5003B8CB8 /* OneSignalWebOpenDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = CA97E14D2051C0A5003B8CB8 /* OneSignalWebOpenDialog.m */; };
CAA4ED0120646762005BD59B /* BadgeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CAA4ED0020646762005BD59B /* BadgeTests.m */; };
CAABF34B205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = CAABF34A205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m */; };
CAABF34C205B157B0042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = CAABF34A205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m */; };
CAABF34D205B157B0042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = CAABF34A205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m */; };
CAEA1C66202BB3C600FBFE9E /* OSEmailSubscription.h in Headers */ = {isa = PBXBuildFile; fileRef = CA810FCF202BA97300A60FED /* OSEmailSubscription.h */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -283,6 +287,9 @@
CA810FD0202BA97300A60FED /* OSEmailSubscription.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSEmailSubscription.m; sourceTree = "<group>"; };
CA97E14C2051C0A5003B8CB8 /* OneSignalWebOpenDialog.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalWebOpenDialog.h; sourceTree = "<group>"; };
CA97E14D2051C0A5003B8CB8 /* OneSignalWebOpenDialog.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalWebOpenDialog.m; sourceTree = "<group>"; };
CAA4ED0020646762005BD59B /* BadgeTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BadgeTests.m; sourceTree = "<group>"; };
CAABF349205B15780042F8E5 /* OneSignalExtensionBadgeHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalExtensionBadgeHandler.h; sourceTree = "<group>"; };
CAABF34A205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalExtensionBadgeHandler.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -406,6 +413,7 @@
children = (
4529DECD1FA81DE000CEAB1D /* Shadows */,
911E2CBC1E398AB3003112A4 /* UnitTests.m */,
CAA4ED0020646762005BD59B /* BadgeTests.m */,
CA63AF8320211F7400E340FB /* EmailTests.m */,
CA63AF8520211FF800E340FB /* UnitTestCommonMethods.h */,
CA63AF8620211FF800E340FB /* UnitTestCommonMethods.m */,
Expand Down Expand Up @@ -452,6 +460,8 @@
4529DF0B1FA932AC00CEAB1D /* OneSignalTrackFirebaseAnalytics.m */,
454F94F01FAD218000D74CCF /* OneSignalNotificationServiceExtensionHandler.h */,
454F94F11FAD218000D74CCF /* OneSignalNotificationServiceExtensionHandler.m */,
CAABF349205B15780042F8E5 /* OneSignalExtensionBadgeHandler.h */,
CAABF34A205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -731,6 +741,7 @@
454F94F51FAD2E5A00D74CCF /* OSNotificationPayload.m in Sources */,
9129C6BE1E89E7AB009CB6A0 /* OSSubscription.m in Sources */,
912412361E73342200E41FD7 /* OneSignalTrackIAP.m in Sources */,
CAABF34B205B15780042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */,
CA08FC7F1FE99B25004C445F /* Requests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -768,6 +779,7 @@
1AF75EB01E8569720097B315 /* NSString+OneSignal.m in Sources */,
9129C6BF1E89E7AB009CB6A0 /* OSSubscription.m in Sources */,
912412371E73342200E41FD7 /* OneSignalTrackIAP.m in Sources */,
CAABF34C205B157B0042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */,
CA08FC801FE99B25004C445F /* Requests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -785,9 +797,11 @@
CA70E3372023D51300019273 /* OneSignalSetEmailParameters.m in Sources */,
4529DED81FA8253D00CEAB1D /* NSUserDefaultsOverrider.m in Sources */,
4529DEED1FA83C5D00CEAB1D /* OneSignalHelperOverrider.m in Sources */,
CAABF34D205B157B0042F8E5 /* OneSignalExtensionBadgeHandler.m in Sources */,
912412301E73342200E41FD7 /* OneSignalSelectorHelpers.m in Sources */,
91F58D851E7C88230017D24D /* OneSignalNotificationSettingsIOS10.m in Sources */,
912412241E73342200E41FD7 /* OneSignalLocation.m in Sources */,
CAA4ED0120646762005BD59B /* BadgeTests.m in Sources */,
912412491E73369800E41FD7 /* OneSignalHelper.m in Sources */,
4529DEE41FA82C6200CEAB1D /* NSURLSessionOverrider.m in Sources */,
4529DED21FA81EA800CEAB1D /* NSObjectOverrider.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions iOS_SDK/OneSignalSDK/Source/OSNotificationPayload.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ -(void)parseCommonOneSignalFields:(NSDictionary*)payload {
_launchURL = payload[@"u"];
_templateID = payload[@"ti"];
_templateName = payload[@"tn"];
_badgeIncrement = [payload[@"badge_inc"] integerValue];
}

-(void)parseApnsFields {
Expand Down
1 change: 1 addition & 0 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ typedef NS_ENUM(NSUInteger, OSNotificationDisplayType) {

/* The badge assigned to the application icon */
@property(readonly)NSUInteger badge;
@property(readonly)NSInteger badgeIncrement;

/* The sound parameter passed to the notification
By default set to UILocalNotificationDefaultSoundName */
Expand Down
20 changes: 17 additions & 3 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

#import "OSObservable.h"

#import "OneSignalExtensionBadgeHandler.h"

#import <stdlib.h>
#import <stdio.h>
#import <sys/types.h>
Expand Down Expand Up @@ -908,14 +910,14 @@ + (void)postNotification:(NSDictionary*)jsonData onSuccess:(OSResultSuccessBlock

onesignal_Log(ONE_S_LL_DEBUG, [NSString stringWithFormat: @"HTTP create notification success %@", jsonResultsString]);
if (successBlock)
successBlock(result);
successBlock(result);
});
} onFailure:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
onesignal_Log(ONE_S_LL_ERROR, @"Create notification failed");
onesignal_Log(ONE_S_LL_INFO, [NSString stringWithFormat: @"%@", error]);
if (failureBlock)
failureBlock(error);
failureBlock(error);
});
}];
}
Expand Down Expand Up @@ -1536,7 +1538,7 @@ + (void)submitNotificationOpened:(NSString*)messageId {

+ (BOOL) clearBadgeCount:(BOOL)fromNotifOpened {

NSNumber *disableBadgeNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"OneSignal_disable_badge_clearing"];
NSNumber *disableBadgeNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:ONESIGNAL_DISABLE_BADGE_CLEARING];

if (disableBadgeNumber)
disableBadgeClearing = [disableBadgeNumber boolValue];
Expand Down Expand Up @@ -1943,9 +1945,21 @@ + (void)load {
// Swizzle - UIApplication delegate
injectToProperClass(@selector(setOneSignalDelegate:), @selector(setDelegate:), @[], [OneSignalAppDelegate class], [UIApplication class]);

injectToProperClass(@selector(onesignalSetApplicationIconBadgeNumber:), @selector(setApplicationIconBadgeNumber:), @[], [OneSignalAppDelegate class], [UIApplication class]);

[self setupUNUserNotificationCenterDelegate];
}

/*
In order for the badge count to be consistent even in situations where the developer manually sets the badge number,
We swizzle the 'setApplicationIconBadgeNumber()' to intercept these calls so we always know the latest count
*/
- (void)onesignalSetApplicationIconBadgeNumber:(NSInteger)badge {
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:badge];

[self onesignalSetApplicationIconBadgeNumber:badge];
}

+(void)setupUNUserNotificationCenterDelegate {
// Swizzle - UNUserNotificationCenter delegate - iOS 10+
if (!NSClassFromString(@"UNUserNotificationCenter"))
Expand Down
44 changes: 37 additions & 7 deletions iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
//
// OneSignalCommonDefines.h
// OneSignal
//
// Created by Brad Hesse on 2/1/18.
// Copyright © 2018 Hiptic. All rights reserved.
//
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef OneSignalCommonDefines_h
#define OneSignalCommonDefines_h
Expand All @@ -28,4 +47,15 @@
#define PROMPT_BEFORE_OPENING_PUSH_URL @"PROMPT_BEFORE_OPENING_PUSH_URL"
#define DEPRECATED_SELECTORS @[@"application:didReceiveLocalNotification:", @"application:handleActionWithIdentifier:forLocalNotification:completionHandler:", @"application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:"]

// Badge handling
#define ONESIGNAL_DISABLE_BADGE_CLEARING @"OneSignal_disable_badge_clearing"
#define ONESIGNAL_APP_GROUP_NAME_KEY @"OneSignal_app_groups_key"
#define ONESIGNAL_BADGE_KEY @"onesignalBadgeCount"

// Firebase
#define ONESIGNAL_FB_ENABLE_FIREBASE @"OS_ENABLE_FIREBASE_ANALYTICS"
#define ONESIGNAL_FB_LAST_TIME_RECEIVED @"OS_LAST_RECIEVED_TIME"
#define ONESIGNAL_FB_LAST_GAF_CAMPAIGN_RECEIVED @"OS_LAST_RECIEVED_GAF_CAMPAIGN"
#define ONESIGNAL_FB_LAST_NOTIFICATION_ID_RECEIVED @"OS_LAST_RECIEVED_NOTIFICATION_ID"

#endif /* OneSignalCommonDefines_h */
37 changes: 37 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OneSignalExtensionBadgeHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import <UserNotifications/UserNotifications.h>
#import "OneSignal.h"

@interface OneSignalExtensionBadgeHandler : NSObject
+ (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request withNotificationPayload:(OSNotificationPayload *)payload withMutableNotificationContent:(UNMutableNotificationContent *)replacementContent;
+ (void)updateCachedBadgeValue:(NSInteger)value;
+ (NSInteger)currentCachedBadgeValue;
+ (NSString *)appGroupName;
@end
85 changes: 85 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OneSignalExtensionBadgeHandler.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#import "OneSignalExtensionBadgeHandler.h"
#import "OneSignalCommonDefines.h"
#import "OneSignalHelper.h"
#import "OneSignalTrackFirebaseAnalytics.h"
#import "OSNotificationPayload+Internal.h"

@implementation OneSignalExtensionBadgeHandler

+ (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request withNotificationPayload:(OSNotificationPayload *)payload withMutableNotificationContent:(UNMutableNotificationContent *)replacementContent {

//if the user is setting the badge directly instead of incrementing/decrementing,
//make sure the OneSignal cached value is updated to this value
if (!payload.badgeIncrement) {
if (payload.badge)
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:payload.badge];

return;
}

var currentValue = (int)OneSignalExtensionBadgeHandler.currentCachedBadgeValue ?: 0;

currentValue += (int)payload.badgeIncrement;

//cannot have negative badge values
if (currentValue < 0)
currentValue = 0;

replacementContent.badge = @(currentValue);

[OneSignalExtensionBadgeHandler updateCachedBadgeValue:currentValue];
}

+ (NSInteger)currentCachedBadgeValue {
let userDefaults = [[NSUserDefaults alloc] initWithSuiteName:OneSignalExtensionBadgeHandler.appGroupName];

return [(NSNumber *)[userDefaults objectForKey:ONESIGNAL_BADGE_KEY] integerValue];
}

+ (void)updateCachedBadgeValue:(NSInteger)value {
//since badge logic can be executed in an extension, we need to use app groups to get
//a shared NSUserDefaults from the app group suite name
let userDefaults = [[NSUserDefaults alloc] initWithSuiteName:OneSignalExtensionBadgeHandler.appGroupName];

[userDefaults setObject:@(value) forKey:ONESIGNAL_BADGE_KEY];

[userDefaults synchronize];
}

+ (NSString *)appGroupName {
var appGroupName = (NSString *)[[NSBundle mainBundle] objectForInfoDictionaryKey:ONESIGNAL_APP_GROUP_NAME_KEY];

if (!appGroupName)
appGroupName = [NSString stringWithFormat:@"group.%@.%@", [[NSBundle mainBundle] bundleIdentifier], @"onesignal"];

return [appGroupName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/

#import "OneSignalNotificationServiceExtensionHandler.h"

#import "OneSignalExtensionBadgeHandler.h"
#import "OneSignalHelper.h"
#import "OneSignalTrackFirebaseAnalytics.h"
#import "OSNotificationPayload+Internal.h"
Expand All @@ -40,6 +40,9 @@ +(UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotifi

let payload = [OSNotificationPayload parseWithApns:request.content.userInfo];

//handle badge count
[OneSignalExtensionBadgeHandler handleBadgeCountWithNotificationRequest:request withNotificationPayload:payload withMutableNotificationContent:replacementContent];

// Track receieved
[OneSignalTrackFirebaseAnalytics trackReceivedEvent:payload];

Expand Down
33 changes: 26 additions & 7 deletions iOS_SDK/OneSignalSDK/Source/OneSignalSetEmailParameters.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
//
// OneSignalSetEmailParameters.h
// OneSignal
//
// Created by Brad Hesse on 2/1/18.
// Copyright © 2018 Hiptic. All rights reserved.
//
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import "OneSignal.h"
Expand Down
Loading