Skip to content

Commit 35ce218

Browse files
authored
Fix for subscribe issue, on cold start always unsubscribed (#599)
* Fix for subscribe issue, on cold start always unsubscribed * During previous refactoring for USerDefault keys an issue was caused with SUBSCRIPTION key * Two essential checks (OneSignal.m and OSSubscription.m) were changed and should not have been * When pausing in app messaging, this only prevented items from being added to the queue, instead of from showing * Demo app changes * Moved all UI property to ViewController.h interface so that the AppDelegate.m has access to them * Added a subscribe segment control (like consent and pausing IAM) to demo app, connected subscription observer * When subscription changes so does the segment control will also * Made segment control selected switches green instead * Fixed white text problem due to default in dark mode * SDK changes * Reverted change for the SUBSCRIPTION key to fix the always unsubscribed issue * Using keyExists in OSSubscription.m now, replaces getSavedObjectForKey and objectForKey * Reverted changes for saving SUBSCRIPTION status in OneSignal.m * Moved the check for paused in app messaging so that queue items won't be shown now * Fixing comments from previous commit in PR * Additional clean-up (sorting and naming) to the OneSignalUserDefault keys * Fixed a unit test after changing functionality of the pause IAM flag * Continuously accumulate IAMs in the queue, but don't show any IAMs until IAMs are enabled again * Fixing comments from previous commit in PR * Changes in OneSignalCommonDefines for OneSignalUserDefault (NSUserDefaults) keys * Now we add suffixes to keys with a to and from state ("_TO" or "_FROM") * Organized and renamed any confusing or inconsistent keys * String values need to be changed still, but we do not know of the affects here and should wait to do anything with the string values until we have a proper migration solution * Removed 2 "saveForBoolKey" methods because they are not using the getter "getBoolForSavedKey" anywhere * To be more specific, OSUD_PERMISSION_ACCEPTED_TO, OSUD_WAS_PROMPTED_FOR_NOTIFICATIONS_TO, OSUD_PROVISIONAL_PUSH_AUTHORIZATION_TO al;l are not used in any setters or getters for UserDefaults * We may need these in the future and we currently use their FROM variants
1 parent 26a4189 commit 35ce218

27 files changed

+403
-402
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
// This project exisits to make testing OneSignal SDK changes.
3030

3131
#import "AppDelegate.h"
32+
#import "ViewController.h"
3233

3334
@implementation AppDelegate
3435

35-
3636
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
3737

3838
// [FIRApp configure];
@@ -64,8 +64,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
6464
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:message];
6565
};
6666

67-
[OneSignal setSubscription:true];
68-
6967
// Example setter for IAM action click handler using OneSignal public method
7068
[OneSignal setInAppMessageClickHandler:inAppMessagingActionClickBlock];
7169

@@ -105,19 +103,18 @@ + (void) setOneSignalAppId:(NSString*)onesignalAppId {
105103
[[NSUserDefaults standardUserDefaults] synchronize];
106104
}
107105

108-
- (void) onOSSubscriptionChanged:(OSSubscriptionStateChanges*)stateChanges {
109-
NSLog(@"onOSSubscriptionChanged: %@", stateChanges);
110-
NSLog(@"HERE");
111-
}
112-
113106
- (void) onOSPermissionChanged:(OSPermissionStateChanges*)stateChanges {
114107
NSLog(@"onOSPermissionChanged: %@", stateChanges);
115-
NSLog(@"HERE");
116108
}
117109

118-
-(void)onOSEmailSubscriptionChanged:(OSEmailSubscriptionStateChanges *)stateChanges {
110+
- (void) onOSSubscriptionChanged:(OSSubscriptionStateChanges*)stateChanges {
111+
NSLog(@"onOSSubscriptionChanged: %@", stateChanges);
112+
ViewController* mainController = (ViewController*) self.window.rootViewController;
113+
mainController.subscriptionSegmentedControl.selectedSegmentIndex = (NSInteger) stateChanges.to.subscribed;
114+
}
115+
116+
- (void)onOSEmailSubscriptionChanged:(OSEmailSubscriptionStateChanges *)stateChanges {
119117
NSLog(@"onOSEmailSubscriptionChanged: %@", stateChanges);
120-
121118
}
122119

123120
- (void)applicationWillResignActive:(UIApplication *)application {

iOS_SDK/OneSignalDevApp/OneSignalDevApp/Base.lproj/Main.storyboard

Lines changed: 145 additions & 158 deletions
Large diffs are not rendered by default.

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,23 @@
3333

3434
@interface ViewController : UIViewController <OSInAppMessageDelegate>
3535

36+
@property (weak, nonatomic) IBOutlet UITextField *appIdTextField;
37+
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
38+
@property (weak, nonatomic) IBOutlet UISegmentedControl *consentSegmentedControl;
39+
@property (weak, nonatomic) IBOutlet UISegmentedControl *subscriptionSegmentedControl;
40+
@property (weak, nonatomic) IBOutlet UISegmentedControl *inAppMessagingSegmentedControl;
41+
@property (weak, nonatomic) IBOutlet UITextField *addTriggerKey;
42+
@property (weak, nonatomic) IBOutlet UITextField *addTriggerValue;
43+
@property (weak, nonatomic) IBOutlet UIButton *addTriggerButton;
44+
@property (weak, nonatomic) IBOutlet UITextField *removeTriggerKey;
45+
@property (weak, nonatomic) IBOutlet UITextField *getTriggerKey;
46+
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
47+
@property (weak, nonatomic) IBOutlet UITextField *externalIdTextField;
48+
@property (weak, nonatomic) IBOutlet UITextField *outcomeName;
49+
@property (weak, nonatomic) IBOutlet UITextField *outcomeValueName;
50+
@property (weak, nonatomic) IBOutlet UITextField *outcomeValue;
51+
@property (weak, nonatomic) IBOutlet UITextField *outcomeUniqueName;
52+
@property (weak, nonatomic) IBOutlet UITextView *result;
53+
3654
@end
3755

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.m

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,6 @@
3131
#import "ViewController.h"
3232
#import "AppDelegate.h"
3333

34-
35-
@interface ViewController ()
36-
@property (weak, nonatomic) IBOutlet UITextField *appIdTextField;
37-
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
38-
@property (weak, nonatomic) IBOutlet UISegmentedControl *consentSegmentedControl;
39-
@property (weak, nonatomic) IBOutlet UISegmentedControl *inAppMessagingSegmentedControl;
40-
@property (weak, nonatomic) IBOutlet UITextField *addTriggerKey;
41-
@property (weak, nonatomic) IBOutlet UITextField *addTriggerValue;
42-
@property (weak, nonatomic) IBOutlet UIButton *addTriggerButton;
43-
@property (weak, nonatomic) IBOutlet UITextField *removeTriggerKey;
44-
@property (weak, nonatomic) IBOutlet UITextField *getTriggerKey;
45-
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
46-
@property (weak, nonatomic) IBOutlet UITextField *externalIdTextField;
47-
@property (weak, nonatomic) IBOutlet UITextField *outcomeName;
48-
@property (weak, nonatomic) IBOutlet UITextField *outcomeValueName;
49-
@property (weak, nonatomic) IBOutlet UITextField *outcomeValue;
50-
@property (weak, nonatomic) IBOutlet UITextField *outcomeUniqueName;
51-
@property (weak, nonatomic) IBOutlet UITextView *result;
52-
53-
@end
54-
5534
@implementation ViewController
5635

5736
- (void)viewDidLoad {
@@ -60,9 +39,11 @@ - (void)viewDidLoad {
6039

6140
self.activityIndicatorView.hidden = true;
6241

63-
self.consentSegmentedControl.selectedSegmentIndex = (NSInteger)![OneSignal requiresUserPrivacyConsent];
42+
self.consentSegmentedControl.selectedSegmentIndex = (NSInteger) ![OneSignal requiresUserPrivacyConsent];
6443

65-
self.inAppMessagingSegmentedControl.selectedSegmentIndex = (NSInteger)![OneSignal isInAppMessagingPaused];
44+
self.subscriptionSegmentedControl.selectedSegmentIndex = (NSInteger) OneSignal.getPermissionSubscriptionState.subscriptionStatus.subscribed;
45+
46+
self.inAppMessagingSegmentedControl.selectedSegmentIndex = (NSInteger) ![OneSignal isInAppMessagingPaused];
6647

6748
self.appIdTextField.text = [AppDelegate getOneSignalAppId];
6849

@@ -102,9 +83,8 @@ - (IBAction)getTriggersAction:(id)sender {
10283
}
10384

10485
- (IBAction)sendTagButton:(id)sender {
105-
//[self promptForNotificationsWithNativeiOS10Code];
106-
107-
//[OneSignal registerForPushNotifications];
86+
// [self promptForNotificationsWithNativeiOS10Code];
87+
// [OneSignal registerForPushNotifications];
10888

10989
[OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
11090
NSLog(@"NEW SDK 2.5.0 METHDO: promptForPushNotificationsWithUserResponse: %d", accepted);
@@ -146,13 +126,18 @@ - (void)didReceiveMemoryWarning {
146126
}
147127

148128
- (IBAction)consentSegmentedControlValueChanged:(UISegmentedControl *)sender {
149-
NSLog(@"View controller consent granted: %i", (int)sender.selectedSegmentIndex);
150-
[OneSignal consentGranted:(bool)sender.selectedSegmentIndex];
129+
NSLog(@"View controller consent granted: %i", (int) sender.selectedSegmentIndex);
130+
[OneSignal consentGranted:(bool) sender.selectedSegmentIndex];
131+
}
132+
133+
- (IBAction)subscriptionSegmentedControlValueChanged:(UISegmentedControl *)sender {
134+
NSLog(@"View controller subscription status: %i", (int) sender.selectedSegmentIndex);
135+
[OneSignal setSubscription:(bool) sender.selectedSegmentIndex];
151136
}
152137

153138
- (IBAction)inAppMessagingSegmentedControlValueChanged:(UISegmentedControl *)sender {
154-
NSLog(@"View controller in app messaging paused: %i", (int)sender.selectedSegmentIndex);
155-
[OneSignal pauseInAppMessages:(bool)sender.selectedSegmentIndex];
139+
NSLog(@"View controller in app messaging paused: %i", (int) !sender.selectedSegmentIndex);
140+
[OneSignal pauseInAppMessages:(bool) !sender.selectedSegmentIndex];
156141
}
157142

158143
-(void)handleMessageAction:(NSString *)actionId {

iOS_SDK/OneSignalSDK/Source/OSAttributedFocusTimeProcessor.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ - (int)getMinSessionTime {
6161
}
6262

6363
- (NSString*)unsentActiveTimeUserDefaultsKey {
64-
return UNSENT_ACTIVE_TIME_ATTRIBUTED;
64+
return OSUD_UNSENT_ACTIVE_TIME_ATTRIBUTED;
6565
}
6666

6767
- (void)sendOnFocusCall:(OSFocusCallParams *)params {

iOS_SDK/OneSignalSDK/Source/OSEmailSubscription.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ - (ObservableEmailSubscriptionStateType *)observable {
4040

4141
- (instancetype)init {
4242
let standardUserDefaults = OneSignalUserDefaults.initStandard;
43-
_emailAddress = [standardUserDefaults getSavedStringForKey:EMAIL_ADDRESS defaultValue:nil];
44-
_requiresEmailAuth = [standardUserDefaults getSavedBoolForKey:REQUIRE_EMAIL_AUTH defaultValue:false];
45-
_emailAuthCode = [standardUserDefaults getSavedStringForKey:EMAIL_AUTH_CODE defaultValue:nil];
46-
_emailUserId = [standardUserDefaults getSavedStringForKey:EMAIL_USERID defaultValue:nil];
43+
_emailAddress = [standardUserDefaults getSavedStringForKey:OSUD_EMAIL_ADDRESS defaultValue:nil];
44+
_requiresEmailAuth = [standardUserDefaults getSavedBoolForKey:OSUD_REQUIRE_EMAIL_AUTH defaultValue:false];
45+
_emailAuthCode = [standardUserDefaults getSavedStringForKey:OSUD_EMAIL_AUTH_CODE defaultValue:nil];
46+
_emailUserId = [standardUserDefaults getSavedStringForKey:OSUD_EMAIL_PLAYER_ID defaultValue:nil];
4747

4848
return self;
4949
}
@@ -54,10 +54,10 @@ -(BOOL)subscribed {
5454

5555
- (void)persist {
5656
let standardUserDefaults = OneSignalUserDefaults.initStandard;
57-
[standardUserDefaults saveStringForKey:EMAIL_ADDRESS withValue:_emailAddress];
58-
[standardUserDefaults saveBoolForKey:REQUIRE_EMAIL_AUTH withValue:_requiresEmailAuth];
59-
[standardUserDefaults saveStringForKey:EMAIL_AUTH_CODE withValue:_emailAuthCode];
60-
[standardUserDefaults saveStringForKey:EMAIL_USERID withValue:_emailUserId];
57+
[standardUserDefaults saveStringForKey:OSUD_EMAIL_ADDRESS withValue:_emailAddress];
58+
[standardUserDefaults saveBoolForKey:OSUD_REQUIRE_EMAIL_AUTH withValue:_requiresEmailAuth];
59+
[standardUserDefaults saveStringForKey:OSUD_EMAIL_AUTH_CODE withValue:_emailAuthCode];
60+
[standardUserDefaults saveStringForKey:OSUD_EMAIL_PLAYER_ID withValue:_emailUserId];
6161
}
6262

6363
- (NSString *)description {

iOS_SDK/OneSignalSDK/Source/OSMessagingController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
NS_ASSUME_NONNULL_BEGIN
3535

3636
@interface OSMessagingController : NSObject <OSInAppMessageViewControllerDelegate, OSTriggerControllerDelegate>
37-
38-
@property (nonatomic) BOOL isInAppMessagingPaused;
37+
38+
@property (class, readonly) BOOL isInAppMessagingPaused;
3939

4040
// Tracks when an IAM is showing or not, useful for deciding to show or hide an IAM
4141
// Toggled in two places dismissing/displaying

iOS_SDK/OneSignalSDK/Source/OSMessagingController.m

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ @interface OSMessagingController ()
5858
@end
5959

6060
@implementation OSMessagingController
61-
@synthesize isInAppMessagingPaused = _isInAppMessagingPaused;
6261

6362
static OSMessagingController *sharedInstance = nil;
6463
static dispatch_once_t once;
65-
6664
+ (OSMessagingController *)sharedInstance {
6765
dispatch_once(&once, ^{
6866
// Make sure only devices with iOS 10 or newer can use IAMs
@@ -79,6 +77,18 @@ + (void)removeInstance {
7977
once = NULL;
8078
}
8179

80+
static BOOL _isInAppMessagingPaused = false;
81+
- (BOOL)isInAppMessagingPaused {
82+
return _isInAppMessagingPaused;
83+
}
84+
- (void)setInAppMessagingPaused:(BOOL)pause {
85+
_isInAppMessagingPaused = pause;
86+
87+
// If IAM are not paused, try to evaluate and show IAMs
88+
if (!pause)
89+
[self evaluateMessages];
90+
}
91+
8292
+ (BOOL)doesDeviceSupportIAM {
8393
// We do not support Mac Catalyst as it does not display correctly.
8494
// We could support in the future after we reslove the display issues.
@@ -110,18 +120,6 @@ - (instancetype)init {
110120
return self;
111121
}
112122

113-
- (BOOL)isInAppMessagingPaused {
114-
return _isInAppMessagingPaused;
115-
}
116-
117-
- (void)setInAppMessagingPaused:(BOOL)pause {
118-
_isInAppMessagingPaused = pause;
119-
120-
// If IAM are not paused, try to evaluate and show IAMs
121-
if (!pause)
122-
[self evaluateMessages];
123-
}
124-
125123
- (void)didUpdateMessagesForSession:(NSArray<OSInAppMessage *> *)newMessages {
126124
self.messages = newMessages;
127125

@@ -133,22 +131,16 @@ - (void)setInAppMessageClickHandler:(OSHandleInAppMessageActionClickBlock)action
133131
}
134132

135133
- (void)presentInAppMessage:(OSInAppMessage *)message {
136-
// Check if the app disabled IAMs for this device
137-
if (_isInAppMessagingPaused)
138-
return;
139-
140134
if (!message.variantId) {
141135
let errorMessage = [NSString stringWithFormat:@"Attempted to display a message with a nil variantId. Current preferred language is %@, supported message variants are %@", NSLocale.preferredLanguages, message.variants];
142136
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:errorMessage];
143137
return;
144138
}
145139

146140
@synchronized (self.messageDisplayQueue) {
147-
// Check if the message already exists in the display queue
148-
if ([self isMessageInDisplayQueue:message.messageId])
149-
return;
150-
151-
[self.messageDisplayQueue addObject:message];
141+
// Add the message to the messageDisplayQueue, if it hasn't been added yet
142+
if (![self isMessageInDisplayQueue:message.messageId])
143+
[self.messageDisplayQueue addObject:message];
152144

153145
// Return early if an IAM is already showing
154146
if (self.isInAppMessageShowing)
@@ -167,28 +159,31 @@ - (BOOL)isMessageInDisplayQueue:(NSString *)messageId {
167159
return false;
168160
}
169161

162+
/*
163+
If an IAM is currently showing add the preview right behind it in the messageDisplayQueue and then dismiss the current IAM
164+
Otherwise, Add it to the front of the messageDisplayQueue and call displayMessage
165+
*/
170166
- (void)presentInAppPreviewMessage:(OSInAppMessage *)message {
171167
@synchronized (self.messageDisplayQueue) {
172-
173-
// If an IAM is currently showing add the preview right behind it in the messageDisplayQueue and then dismiss the current IAM
174-
// Otherwise, Add it to the front of the messageDisplayQueue and call displayMessage
175168
if (self.isInAppMessageShowing) {
176-
177-
// Add preview behind current displaying IAM in messageDisplayQueue
169+
// Add preview second in messageDisplayQueue
178170
[self.messageDisplayQueue insertObject:message atIndex:1];
179-
// Get current OSInAppMessageViewController and dismiss current IAM showing using dismissMessageWithDirection method
180171
[self.viewController dismissCurrentInAppMessage];
181172
} else {
182-
183173
// Add preview to front of messageDisplayQueue
184174
[self.messageDisplayQueue insertObject:message atIndex:0];
185-
// Show new IAM preview
186175
[self displayMessage:message];
187176
}
188177
};
189178
}
190179

191180
- (void)displayMessage:(OSInAppMessage *)message {
181+
// Check if the app disabled IAMs for this device before showing an IAM
182+
if (_isInAppMessagingPaused) {
183+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"In app messages will not show while paused"];
184+
return;
185+
}
186+
192187
self.isInAppMessageShowing = true;
193188

194189
self.viewController = [[OSInAppMessageViewController alloc] initWithMessage:message delegate:self];
@@ -204,7 +199,6 @@ - (void)displayMessage:(OSInAppMessage *)message {
204199
Request should only be made for IAMs that are not previews and have not been impressioned yet
205200
*/
206201
- (void)messageViewImpressionRequest:(OSInAppMessage *)message {
207-
208202
// Make sure no tracking is performed for previewed IAMs
209203
// If the messageId exists in cached impressionedInAppMessages return early so the impression is not tracked again
210204
if (message.isPreview || [self.impressionedInAppMessages containsObject:message.messageId])
@@ -240,6 +234,7 @@ - (void)messageViewImpressionRequest:(OSInAppMessage *)message {
240234
Checks to see if any messages should be shown now
241235
*/
242236
- (void)evaluateMessages {
237+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"Evaluating in app messages"];
243238
for (OSInAppMessage *message in self.messages) {
244239
// Should we show the in app message
245240
if ([self shouldShowInAppMessage:message]) {

iOS_SDK/OneSignalSDK/Source/OSOutcomeEventsDefines.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
#define OSOutcomeEventsDefines_h
3030

3131
// Outcome param keys
32-
static NSString * const OUTCOMES_PARAM = @"outcomes";
33-
static NSString * const DIRECT_PARAM = @"direct";
34-
static NSString * const INDIRECT_PARAM = @"indirect";
35-
static NSString * const UNATTRIBUTED_PARAM = @"unattributed";
36-
static NSString * const ENABLED_PARAM = @"enabled";
37-
static NSString * const NOTIFICATION_ATTRIBUTION_PARAM = @"notification_attribution";
38-
static NSString * const MINUTES_SINCE_DISPLAYED_PARAM = @"minutes_since_displayed";
39-
static NSString * const LIMIT_PARAM = @"limit";
32+
static NSString* const OUTCOMES_PARAM = @"outcomes";
33+
static NSString* const DIRECT_PARAM = @"direct";
34+
static NSString* const INDIRECT_PARAM = @"indirect";
35+
static NSString* const UNATTRIBUTED_PARAM = @"unattributed";
36+
static NSString* const ENABLED_PARAM = @"enabled";
37+
static NSString* const NOTIFICATION_ATTRIBUTION_PARAM = @"notification_attribution";
38+
static NSString* const MINUTES_SINCE_DISPLAYED_PARAM = @"minutes_since_displayed";
39+
static NSString* const LIMIT_PARAM = @"limit";
4040

4141
// Outcome default param values
4242
static int DEFAULT_INDIRECT_NOTIFICATION_LIMIT = 10;

0 commit comments

Comments
 (0)