Skip to content

Commit 94e1844

Browse files
committed
Notifications Manager - push token and sub ID from cache
* The `OSNotificationsManager` needs push token and push subscription ID, when it may not be passed to it, if it already exists. * Get these two values from the cache when they are accessed and they are nil. * Previously, When the app starts up, the _pushToken would always be `nil` and return a negative number incorrectly. * Also fix a bug where we send a nil pushToken to the user module, it will translate as "", and register as a change when it really isn't
1 parent 239245e commit 94e1844

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,26 @@ + (OSPermissionStateInternal*)lastPermissionState {
178178
return _lastPermissionState;
179179
}
180180

181+
// TODO: pushToken, pushSubscriptionId needs to be available... is this the right setup
181182
static NSString *_pushToken;
183+
+ (NSString*)pushToken {
184+
if (!_pushToken) {
185+
_pushToken = [OneSignalUserDefaults.initShared getSavedStringForKey:OSUD_PUSH_TOKEN_TO defaultValue:nil];
186+
}
187+
return _pushToken;
188+
}
182189

183190
static NSString *_pushSubscriptionId;
191+
+ (NSString*)pushSubscriptionId {
192+
if (!_pushSubscriptionId) {
193+
_pushSubscriptionId = [OneSignalUserDefaults.initShared getSavedStringForKey:OSUD_PLAYER_ID_TO defaultValue:nil];
194+
}
195+
return _pushSubscriptionId;
196+
}
184197
+ (void)setPushSubscriptionId:(NSString *)pushSubscriptionId {
185198
_pushSubscriptionId = pushSubscriptionId;
186199
}
200+
187201
#pragma clang diagnostic push
188202
#pragma clang diagnostic ignored "-Wundeclared-selector"
189203
+ (void)start {
@@ -377,6 +391,9 @@ + (void)didRegisterForRemoteNotifications:(UIApplication *)app
377391
self.waitingForApnsResponse = false;
378392

379393
_pushToken = parsedDeviceToken;
394+
395+
// Cache push token
396+
[OneSignalUserDefaults.initShared saveStringForKey:OSUD_PUSH_TOKEN_TO withValue:_pushToken];
380397

381398
[self sendPushTokenToDelegate];
382399
}
@@ -399,7 +416,8 @@ + (void)handleDidFailRegisterForRemoteNotification:(NSError*)err {
399416
}
400417

401418
+ (void)sendPushTokenToDelegate {
402-
if (self.delegate && [self.delegate respondsToSelector:@selector(setPushToken:)]) {
419+
// TODO: Keep this as a check on _pushToken instead of self.pushToken?
420+
if (_pushToken != nil && self.delegate && [self.delegate respondsToSelector:@selector(setPushToken:)]) {
403421
[self.delegate setPushToken:_pushToken];
404422
}
405423
}
@@ -469,7 +487,8 @@ + (int)getNotificationTypes {
469487
if (mSubscriptionStatus < -9)
470488
return mSubscriptionStatus;
471489

472-
if (OSNotificationsManager.waitingForApnsResponse && !_pushToken)
490+
// This was previously nil if just accessing _pushToken
491+
if (OSNotificationsManager.waitingForApnsResponse && !self.pushToken)
473492
return ERROR_PUSH_DELEGATE_NEVER_FIRED;
474493

475494
OSPermissionStateInternal* permissionStatus = [OSNotificationsManager.osNotificationSettings getNotificationPermissionState];

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSSubscriptionModel.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ class OSSubscriptionModel: OSModel {
100100
return
101101
}
102102

103-
// Cache the push token as it persists across users on the device?
104-
OneSignalUserDefaults.initShared().saveString(forKey: OSUD_PUSH_TOKEN_TO, withValue: address)
105-
106103
updateNotificationTypes()
107104

108105
firePushSubscriptionChanged(.address(oldValue))

0 commit comments

Comments
 (0)