Skip to content

Commit bb4ca5e

Browse files
committed
Fixes to declined on first init, updates from settings, and session count
* Fixed registration issue where the app already prompted the user in a previous app version before OneSignal was added and they declined. * Fixed issue where changes to notification permissions from Settings would not update when coming back to the app. * Fixed sessions not tracking when the app is resumed. - Only cold starts were being tracked. * Some of the above issues may have not existed in a released version.
1 parent 0639be3 commit bb4ca5e

File tree

3 files changed

+165
-37
lines changed

3 files changed

+165
-37
lines changed

iOS_SDK/OneSignal/OneSignal.m

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@
8282
/* Omit no app_id error logging, for use with wrapper SDKs. */
8383
NSString* const kOSSettingsKeyInOmitNoAppIdLogging = @"kOSSettingsKeyInOmitNoAppIdLogging";
8484

85+
86+
@interface OSSubcscriptionStatus : NSObject
87+
@property BOOL anwseredPrompt;
88+
@property BOOL accepted;
89+
@end
90+
91+
@implementation OSSubcscriptionStatus
92+
@end
93+
8594
@implementation OneSignal
8695

8796
NSString* const ONESIGNAL_VERSION = @"020305";
@@ -171,7 +180,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
171180
[OneSignalLocation getLocation:false];
172181

173182
if (self) {
174-
UIApplication* sharedApp = [UIApplication sharedApplication];
175183
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
176184

177185
httpClient = [[OneSignalHTTPClient alloc] init];
@@ -247,10 +255,14 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
247255

248256
if (mUserId != nil)
249257
[self registerUser];
250-
else // Fall back in case Apple does not responds in time.
251-
[self performSelector:@selector(registerUser) withObject:nil afterDelay:30.0f];
252-
253-
[OneSignalTracker onFocus:NO];
258+
else {
259+
[self userAnsweredNotificationPrompt:^(OSSubcscriptionStatus *status) {
260+
if (status.anwseredPrompt)
261+
[self registerUser];
262+
else
263+
[self performSelector:@selector(registerUser) withObject:nil afterDelay:30.0f];
264+
}];
265+
}
254266
}
255267

256268
/*
@@ -655,8 +667,8 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
655667
// iOS 8 - We get a token right away but give the user 30 sec to respond to the system prompt.
656668
// Also check notification types so there is no waiting if user has already answered the system prompt.
657669
// The goal is to only have 1 server call.
658-
[self userAnsweredNotificationPrompt:^(BOOL anwsered) {
659-
if (anwsered)
670+
[self userAnsweredNotificationPrompt:^(OSSubcscriptionStatus *status) {
671+
if (status.anwseredPrompt)
660672
[OneSignal registerUser];
661673
else {
662674
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(registerUser) object:nil];
@@ -677,9 +689,12 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
677689
NSMutableURLRequest* request;
678690
request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", mUserId]];
679691

692+
int notificationTypes = [self getNotificationTypes];
693+
680694
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
681695
app_id, @"app_id",
682696
deviceToken, @"identifier",
697+
[NSNumber numberWithInt:notificationTypes], @"notification_types",
683698
nil];
684699

685700
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"Calling OneSignal PUT updated pushToken!"];
@@ -718,12 +733,12 @@ +(BOOL)shouldRegisterNow {
718733
if (waitingForOneSReg)
719734
return false;
720735

721-
//Figure out if should pass or not
736+
// Figure out if should pass or not
722737
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
723738
NSTimeInterval lastTimeClosed = [[NSUserDefaults standardUserDefaults] doubleForKey:@"GT_LAST_CLOSED_TIME"];
724739
if (!lastTimeClosed) {
725740
[self updateLastSessionDateTime];
726-
return YES;
741+
return true;
727742
}
728743

729744
if ([self isHighPriorityCall])
@@ -881,10 +896,14 @@ +(NSString*) getUsableDeviceToken {
881896
}
882897

883898
// Updates the server with the new user's notification setting or subscription status changes
884-
+ (void) sendNotificationTypesUpdate {
885-
899+
+ (BOOL) sendNotificationTypesUpdate {
886900
// User changed notification settings for the app.
887901
if ([self getNotificationTypes] != -1 && mUserId && mLastNotificationTypes != [self getNotificationTypes]) {
902+
if (mDeviceToken == nil) {
903+
[self registerForAPNsToken];
904+
return true;
905+
}
906+
888907
mLastNotificationTypes = [self getNotificationTypes];
889908
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", mUserId]];
890909
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
@@ -900,8 +919,11 @@ + (void) sendNotificationTypesUpdate {
900919
idsAvailableBlockWhenReady(mUserId, [self getUsableDeviceToken]);
901920
idsAvailableBlockWhenReady = nil;
902921
}
922+
923+
return true;
903924
}
904925

926+
return false;
905927
}
906928

907929
+ (void)sendPurchases:(NSArray*)purchases {
@@ -1127,12 +1149,10 @@ + (int) getNotificationTypes {
11271149
if (!mSubscriptionSet)
11281150
return -2;
11291151

1130-
if (mDeviceToken) {
1131-
if ([OneSignalHelper canGetNotificationTypes])
1132-
return [[UIApplication sharedApplication] currentUserNotificationSettings].types;
1133-
else
1134-
return NOTIFICATION_TYPE_ALL;
1135-
}
1152+
if ([OneSignalHelper canGetNotificationTypes])
1153+
return [[UIApplication sharedApplication] currentUserNotificationSettings].types;
1154+
else if (mDeviceToken)
1155+
return NOTIFICATION_TYPE_ALL;
11361156

11371157
return 0;
11381158
}
@@ -1142,21 +1162,34 @@ + (void)setSubscriptionStatus:(int)errorType {
11421162
[self sendNotificationTypesUpdate];
11431163
}
11441164

1145-
+ (void)userAnsweredNotificationPrompt:(void (^)(BOOL anwsered))completionHandler {
1165+
+ (void)userAnsweredNotificationPrompt:(void (^)(OSSubcscriptionStatus *anwsered))completionHandler {
11461166
if ([OneSignalHelper isiOS10Plus]) {
11471167
Class unUserNotifClass = NSClassFromString(@"UNUserNotificationCenter");
11481168
[[unUserNotifClass currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(id settings) {
11491169
// Trigger callback on the main thread.
11501170
// Prevents any possiblitity creating thread locks by callling currentUserNotification from this block.
11511171
dispatch_async(dispatch_get_main_queue(), ^{
11521172
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message: [NSString stringWithFormat:@"getNotificationSettingsWithCompletionHandler Called: %@", settings]];
1153-
completionHandler([settings authorizationStatus] != 0);
1173+
OSSubcscriptionStatus *status = [OSSubcscriptionStatus alloc];
1174+
status.anwseredPrompt = [settings authorizationStatus] != 0;
1175+
status.accepted = [settings authorizationStatus] == 2;
1176+
completionHandler(status);
11541177
});
11551178
}];
11561179
}
11571180
else {
11581181
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
1159-
completionHandler([userDefaults boolForKey:@"OS_NOTIFICATION_PROMPT_ANSWERED"]);
1182+
OSSubcscriptionStatus *status = [OSSubcscriptionStatus alloc];
1183+
status.anwseredPrompt = [userDefaults boolForKey:@"OS_NOTIFICATION_PROMPT_ANSWERED"];
1184+
1185+
if ([OneSignalHelper canGetNotificationTypes]) {
1186+
status.accepted = [[UIApplication sharedApplication] currentUserNotificationSettings].types > 0;
1187+
completionHandler(status);
1188+
}
1189+
else {
1190+
status.accepted = mDeviceToken != nil;
1191+
completionHandler(status);
1192+
}
11601193
}
11611194
}
11621195

iOS_SDK/OneSignalTracker.m

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
@interface OneSignal ()
3636

3737
+ (void)registerUser;
38-
+ (void) sendNotificationTypesUpdate;
39-
+ (BOOL) clearBadgeCount:(BOOL)fromNotifOpened;
38+
+ (BOOL)sendNotificationTypesUpdate;
39+
+ (BOOL)clearBadgeCount:(BOOL)fromNotifOpened;
4040
+ (NSString*)mUserId;
4141

4242
@end
@@ -61,8 +61,8 @@ + (void) endBackgroundFocusTask {
6161

6262
+ (void)onFocus:(BOOL)toBackground {
6363

64-
//Prevent the onFocus to be called twice when app being terminated
65-
// (Both WillResignActive and willTerminate
64+
// Prevent the onFocus to be called twice when app being terminated
65+
// - Both WillResignActive and willTerminate
6666
if (lastOnFocusWasToBackground == toBackground)
6767
return;
6868
lastOnFocusWasToBackground = toBackground;
@@ -73,13 +73,7 @@ + (void)onFocus:(BOOL)toBackground {
7373
NSTimeInterval timeToPingWith = 0.0;
7474

7575

76-
if (!toBackground) {
77-
lastOpenedTime = now;
78-
[OneSignal sendNotificationTypesUpdate];
79-
wasBadgeSet = [OneSignal clearBadgeCount:false];
80-
}
81-
else {
82-
76+
if (toBackground) {
8377
[[NSUserDefaults standardUserDefaults] setDouble:now forKey:@"GT_LAST_CLOSED_TIME"];
8478
[[NSUserDefaults standardUserDefaults] synchronize];
8579

@@ -96,7 +90,15 @@ + (void)onFocus:(BOOL)toBackground {
9690
}
9791

9892
timeToPingWith = totalTimeActive;
93+
}
94+
else {
95+
lastOpenedTime = now;
96+
BOOL firedUpdate = [OneSignal sendNotificationTypesUpdate];
9997

98+
// on_session tracking when resumming app.
99+
if (!firedUpdate)
100+
[OneSignal registerUser];
101+
wasBadgeSet = [OneSignal clearBadgeCount:false];
100102
}
101103

102104
if (![OneSignal mUserId])

0 commit comments

Comments
 (0)