Skip to content

Commit 088d637

Browse files
authored
Merge pull request #1203 from OneSignal/user_model/fetch_user_replaces_user_data
[user model] replace user data with the fetch user response
2 parents 61ad43c + 0fff0db commit 088d637

File tree

10 files changed

+86
-40
lines changed

10 files changed

+86
-40
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
3C2C7DC6288E00AA0020F9AE /* UserModelObjcTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C2C7DC5288E00AA0020F9AE /* UserModelObjcTests.m */; };
6969
3C2C7DC8288F3C020020F9AE /* OSSubscriptionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */; };
7070
3C2D8A5928B4C4E300BE41F6 /* OSDelta.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */; };
71+
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
72+
3C44673F296D09CC0039A49E /* OneSignalMobileProvision.h in Headers */ = {isa = PBXBuildFile; fileRef = 912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */; settings = {ATTRIBUTES = (Public, ); }; };
7173
3C448B9D2936ADFD002F96BC /* OSBackgroundTaskManagerImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C448B9B2936ADFD002F96BC /* OSBackgroundTaskManagerImpl.h */; };
7274
3C448B9E2936ADFD002F96BC /* OSBackgroundTaskManagerImpl.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C448B9C2936ADFD002F96BC /* OSBackgroundTaskManagerImpl.m */; };
7375
3C448B9F2936ADFD002F96BC /* OSBackgroundTaskManagerImpl.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C448B9C2936ADFD002F96BC /* OSBackgroundTaskManagerImpl.m */; };
@@ -182,10 +184,6 @@
182184
912412221E73342200E41FD7 /* OneSignalLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FB1E73342200E41FD7 /* OneSignalLocation.m */; };
183185
912412231E73342200E41FD7 /* OneSignalLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FB1E73342200E41FD7 /* OneSignalLocation.m */; };
184186
912412241E73342200E41FD7 /* OneSignalLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FB1E73342200E41FD7 /* OneSignalLocation.m */; };
185-
912412251E73342200E41FD7 /* OneSignalMobileProvision.h in Headers */ = {isa = PBXBuildFile; fileRef = 912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */; };
186-
912412261E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
187-
912412271E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
188-
912412281E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
189187
912412311E73342200E41FD7 /* OneSignalTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 912412021E73342200E41FD7 /* OneSignalTracker.h */; };
190188
912412321E73342200E41FD7 /* OneSignalTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = 912412031E73342200E41FD7 /* OneSignalTracker.m */; };
191189
912412331E73342200E41FD7 /* OneSignalTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = 912412031E73342200E41FD7 /* OneSignalTracker.m */; };
@@ -1447,8 +1445,6 @@
14471445
912411F91E73342200E41FD7 /* OneSignalJailbreakDetection.m */,
14481446
912411FA1E73342200E41FD7 /* OneSignalLocation.h */,
14491447
912411FB1E73342200E41FD7 /* OneSignalLocation.m */,
1450-
912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */,
1451-
912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */,
14521448
912412021E73342200E41FD7 /* OneSignalTracker.h */,
14531449
912412031E73342200E41FD7 /* OneSignalTracker.m */,
14541450
912412041E73342200E41FD7 /* OneSignalTrackIAP.h */,
@@ -1646,6 +1642,8 @@
16461642
CA70E3382023F24500019273 /* OneSignalCommonDefines.h */,
16471643
7AD8DDE8234BD3CF00747A8A /* OneSignalUserDefaults.h */,
16481644
7AD8DDE6234BD3BE00747A8A /* OneSignalUserDefaults.m */,
1645+
912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */,
1646+
912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */,
16491647
4529DF0A1FA932AC00CEAB1D /* OneSignalTrackFirebaseAnalytics.h */,
16501648
4529DF0B1FA932AC00CEAB1D /* OneSignalTrackFirebaseAnalytics.m */,
16511649
DE7D1831270279D9002D3A5D /* OSNotificationClasses.h */,
@@ -1998,7 +1996,6 @@
19981996
A63E9E3F26742C1400EA273E /* LanguageProvider.h in Headers */,
19991997
A66239952686612F00D52FD8 /* OneSignalFramework.h in Headers */,
20001998
7A93269325AF4E6700BBEC27 /* OSPendingCallbacks.h in Headers */,
2001-
912412251E73342200E41FD7 /* OneSignalMobileProvision.h in Headers */,
20021999
A63E9E3E26742C1000EA273E /* LanguageContext.h in Headers */,
20032000
CA4742E4218B8FF30020DC8C /* OSTriggerController.h in Headers */,
20042001
DE7D18EC2703B5AA002D3A5D /* OSInAppMessagingRequests.h in Headers */,
@@ -2072,6 +2069,7 @@
20722069
3CE8CC4E2911ADD1000DB0D3 /* OSDeviceUtils.h in Headers */,
20732070
3C47A974292642B100312125 /* OneSignalConfigManager.h in Headers */,
20742071
DE7D183627027AA0002D3A5D /* OneSignalLog.h in Headers */,
2072+
3C44673F296D09CC0039A49E /* OneSignalMobileProvision.h in Headers */,
20752073
DE7D182D270273B0002D3A5D /* OSNotification.h in Headers */,
20762074
DE7D183F27027F62002D3A5D /* NSString+OneSignal.h in Headers */,
20772075
);
@@ -2582,7 +2580,6 @@
25822580
9D3300F523145AF3000F0A83 /* OneSignalViewHelper.m in Sources */,
25832581
9124123E1E73342200E41FD7 /* UIApplicationDelegate+OneSignal.m in Sources */,
25842582
CA47439E2190FEA80020DC8C /* OSTrigger.m in Sources */,
2585-
912412261E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */,
25862583
CAB4112920852E48005A70D1 /* DelayedConsentInitializationParameters.m in Sources */,
25872584
CA1A6E7020DC2E73001C41B9 /* OneSignalDialogRequest.m in Sources */,
25882585
912412321E73342200E41FD7 /* OneSignalTracker.m in Sources */,
@@ -2658,7 +2655,6 @@
26582655
CA1A6E7120DC2E73001C41B9 /* OneSignalDialogRequest.m in Sources */,
26592656
CA4742E6218B8FF30020DC8C /* OSTriggerController.m in Sources */,
26602657
CAB269E121B2038B00F8A43C /* OSInAppMessageBridgeEvent.m in Sources */,
2661-
912412271E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */,
26622658
912412331E73342200E41FD7 /* OneSignalTracker.m in Sources */,
26632659
DE367CC824EEF2BE00165207 /* OSInAppMessagePage.m in Sources */,
26642660
DE7D18EF2703B5B9002D3A5D /* OSInAppMessagingRequests.m in Sources */,
@@ -2765,7 +2761,6 @@
27652761
03CCCC852835F291004BF794 /* UIApplicationDelegateSwizzlingTests.m in Sources */,
27662762
4529DEEA1FA8360C00CEAB1D /* UIApplicationOverrider.m in Sources */,
27672763
DEC08B022947D4E900C81DA3 /* OneSignalSwiftInterface.swift in Sources */,
2768-
912412281E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */,
27692764
7A93269E25AF4F0300BBEC27 /* OSPendingCallbacks.m in Sources */,
27702765
7AECE59823674AB700537907 /* OSUnattributedFocusTimeProcessor.m in Sources */,
27712766
7A5A818224897693002E07C8 /* MigrationTests.m in Sources */,
@@ -2817,6 +2812,7 @@
28172812
DEF784642912FA5100A1F3A5 /* OSDialogInstanceManager.m in Sources */,
28182813
DE7D183B27027EFC002D3A5D /* NSURL+OneSignal.m in Sources */,
28192814
DE7D186B270374EE002D3A5D /* OneSignalRequest.m in Sources */,
2815+
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */,
28202816
DEF78492291479B200A1F3A5 /* OneSignalSelectorHelpers.m in Sources */,
28212817
DE7D182B27027376002D3A5D /* OSNotification.m in Sources */,
28222818
DE7D187A27037A26002D3A5D /* OneSignalCoreHelper.m in Sources */,

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@
5252
#import <OneSignalCore/OneSignalSelectorHelpers.h>
5353
#import <OneSignalCore/OneSignalConfigManager.h>
5454
#import <OneSignalCore/OSRemoteParamController.h>
55-
55+
#import <OneSignalCore/OneSignalMobileProvision.h>

iOS_SDK/OneSignalSDK/Source/OneSignalMobileProvision.h renamed to iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalMobileProvision.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright (c) 2013 The Blindsight Corporation. All rights reserved.
77
// Released under the BSD 2-Clause License (see LICENSE)
88

9-
typedef NS_ENUM(NSInteger, UIApplicationReleaseMode) {
9+
typedef NS_ENUM(NSInteger, OSUIApplicationReleaseMode) {
1010
UIApplicationReleaseUnknown,
1111
UIApplicationReleaseDev,
1212
UIApplicationReleaseAdHoc,
@@ -18,6 +18,6 @@ typedef NS_ENUM(NSInteger, UIApplicationReleaseMode) {
1818

1919
@interface OneSignalMobileProvision : NSObject
2020

21-
+ (UIApplicationReleaseMode) releaseMode;
21+
+ (OSUIApplicationReleaseMode) releaseMode;
2222

2323
@end

iOS_SDK/OneSignalSDK/Source/OneSignalMobileProvision.m renamed to iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalMobileProvision.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
// Created by kaolin fire on 2013-06-24.
66
// Copyright (c) 2013 The Blindsight Corporation. All rights reserved.
77
// Released under the BSD 2-Clause License (see LICENSE)
8-
#import <UIKit/UIKit.h>
98
#import <Foundation/Foundation.h>
109

1110
#import "OneSignalMobileProvision.h"
12-
#import "TargetConditionals.h"
13-
#import "OneSignalFramework.h"
14-
#import "OneSignalInternal.h"
11+
#import "OneSignalLog.h"
1512

1613
@implementation OneSignalMobileProvision
1714

@@ -86,7 +83,7 @@ + (void)logInvalidProvisionError:(NSString *)message {
8683
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:message];
8784
}
8885

89-
+ (UIApplicationReleaseMode) releaseMode {
86+
+ (OSUIApplicationReleaseMode) releaseMode {
9087
NSDictionary *entitlements = nil;
9188
NSDictionary *provision = [self getProvision];
9289
if (provision) {

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ + (void)setColdStartFromTapOnNotification:(BOOL)coldStartFromTapOnNotification {
131131
_coldStartFromTapOnNotification = coldStartFromTapOnNotification;
132132
}
133133

134-
static NSString *_appId;
135-
+ (void)setAppId:(NSString *)appId {
136-
_appId = appId;
137-
}
138-
+ (NSString *_Nullable)getAppId {
139-
return _appId;
140-
}
141-
142134
+ (void)setMSubscriptionStatus:(NSNumber*)status {
143135
mSubscriptionStatus = [status intValue];
144136
}
@@ -537,8 +529,9 @@ + (void)notificationReceived:(NSDictionary*)messageDict wasOpened:(BOOL)opened {
537529
if ([OSPrivacyConsentController shouldLogMissingPrivacyConsentErrorWithMethodName:nil])
538530
return;
539531

540-
if (!_appId)
532+
if (![OneSignalConfigManager getAppId]) {
541533
return;
534+
}
542535

543536
// This method should not continue to be executed for non-OS push notifications
544537
if (![OneSignalCoreHelper isOneSignalPayload:messageDict])
@@ -670,8 +663,8 @@ + (void)submitNotificationOpened:(NSString*)messageId {
670663
NSString* lastMessageId = [standardUserDefaults getSavedStringForKey:OSUD_LAST_MESSAGE_OPENED defaultValue:nil];
671664
//Only submit request if messageId not nil and: (lastMessage is nil or not equal to current one)
672665
if(messageId && (!lastMessageId || ![lastMessageId isEqualToString:messageId])) {
673-
[OneSignalClient.sharedClient executeRequest:[OSRequestSubmitNotificationOpened withUserId:_pushSubscriptionId
674-
appId:_appId
666+
[OneSignalClient.sharedClient executeRequest:[OSRequestSubmitNotificationOpened withUserId:[self pushSubscriptionId]
667+
appId:[OneSignalConfigManager getAppId]
675668
wasOpened:YES
676669
messageId:messageId
677670
withDeviceType:[NSNumber numberWithInt:DEVICE_TYPE_PUSH]]

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSIdentityModel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class OSIdentityModel: OSModel {
6565
self.aliases = aliases
6666
}
6767

68+
/**
69+
Called to clear the model's data in preparation for hydration via a fetch user call.
70+
*/
71+
func clearData() {
72+
self.aliases = [:]
73+
}
74+
6875
// MARK: - Alias Methods
6976

7077
func addAliases(_ aliases: [String: String]) {

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSPropertiesModel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class OSPropertiesModel: OSModel {
9292
// ... and more
9393
}
9494

95+
/**
96+
Called to clear the model's data in preparation for hydration via a fetch user call.
97+
*/
98+
func clearData() {
99+
// TODO: What about language, lat, long?
100+
self.tags = [:]
101+
}
102+
95103
// MARK: - Tag Methods
96104

97105
func addTags(_ tags: [String: String]) {

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSSubscriptionModel.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ class OSSubscriptionModel: OSModel {
186186
}
187187
}
188188

189+
// Properties for push subscription
190+
var testType: Int?
191+
let deviceOs = UIDevice.current.systemVersion
192+
let sdk = ONESIGNAL_VERSION
193+
let deviceModel: String? = OSDeviceUtils.getDeviceVariant()
194+
189195
// When a Subscription is initialized, it may not have a subscriptionId until a request to the backend is made.
190196
init(type: OSSubscriptionType,
191197
address: String?,
@@ -198,6 +204,22 @@ class OSSubscriptionModel: OSModel {
198204
self.subscriptionId = subscriptionId
199205
_accepted = accepted
200206
_isDisabled = isDisabled
207+
208+
// Set test_type if subscription model is PUSH
209+
if type == .push {
210+
let releaseMode: OSUIApplicationReleaseMode = OneSignalMobileProvision.releaseMode()
211+
// Workaround to unsure how to extract the Int value in 1 step...
212+
if releaseMode == .UIApplicationReleaseDev {
213+
self.testType = OSUIApplicationReleaseMode.UIApplicationReleaseDev.rawValue
214+
}
215+
if releaseMode == .UIApplicationReleaseAdHoc {
216+
self.testType = OSUIApplicationReleaseMode.UIApplicationReleaseAdHoc.rawValue
217+
}
218+
if releaseMode == .UIApplicationReleaseWildcard {
219+
self.testType = OSUIApplicationReleaseMode.UIApplicationReleaseWildcard.rawValue
220+
}
221+
}
222+
201223
super.init(changeNotifier: changeNotifier)
202224
}
203225

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSUserRequests.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class OSUserExecutor {
6666
Used to parse Create User and Fetch User responses. The `originalPushToken` is the push token when the request was created, which may be different from the push token currently in the SDK. For example, when the request was created, there may be no push token yet, but soon after, the SDK receives a push token. This is used to determine whether or not to hydrate the push subscription.
6767
*/
6868
static func parseFetchUserResponse(response: [AnyHashable: Any], identityModel: OSIdentityModel, originalPushToken: String?) {
69+
70+
// If this was a create user, it hydrates the onesignal_id of the request's identityModel
71+
// The model in the store may be different, and it may be waiting on the onesignal_id of this previous model
72+
if let identityObject = parseIdentityObjectResponse(response) {
73+
identityModel.hydrate(identityObject)
74+
}
75+
6976
// On success, check if the current user is the same as the one in the request
7077
// If user has changed, don't hydrate, except for push subscription
7178
let modelInStore = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(key: OS_IDENTITY_MODEL_KEY)
@@ -167,13 +174,14 @@ class OSUserExecutor {
167174
return
168175
}
169176
OneSignalClient.shared().execute(request) { response in
170-
// TODO: Differentiate if we need to fetch based on response code 200, 201, 202
171-
// Create User's response won't send us the user's complete info if this user already existed.
172-
// We can parse the response OR fetch user (and then parse response then)
173-
// For now, do both, as we should parse to get the subscription_id from this request
177+
// TODO: Differentiate if we need to fetch the user based on response code of 200, 201, 202
178+
// Create User's response won't send us the user's complete info if this user already exists
174179
if let response = response {
180+
// Parse the response for any data we need to update
175181
parseFetchUserResponse(response: response, identityModel: request.identityModel, originalPushToken: originalPushToken)
176-
// If we logged into an external_id, fetch the user data
182+
183+
// If this user already exists and we logged into an external_id, fetch the user data
184+
// TODO: Only do this if response code is 200 or 202
177185
if let identity = request.parameters?["identity"] as? [String: String],
178186
let externalId = identity[OS_EXTERNAL_ID] {
179187
fetchUser(aliasLabel: OS_EXTERNAL_ID, aliasId: externalId, identityModel: request.identityModel)
@@ -208,12 +216,8 @@ class OSUserExecutor {
208216

209217
static func executeIdentifyUserRequest(_ request: OSRequestIdentifyUser) {
210218
OneSignalClient.shared().execute(request) { _ in
211-
// the anonymous user has been identified, still need to Fetch User
212-
// TODO: Is the above true, do we need to Fetch? If the anon user is identified, then no user with this external_id existed, correct?
213-
fetchUser(aliasLabel: OS_EXTERNAL_ID, aliasId: request.aliasId, identityModel: request.identityModelToUpdate)
214-
215-
executePendingRequests() // TODO: Here or after fetch or after transfer?
216-
219+
// the anonymous user has been identified, no further action needed, no need to fetch user
220+
executePendingRequests()
217221
} onFailure: { error in
218222
// Returns 409 if any provided (label, id) pair exists on another User, so the SDK will switch to this user.
219223
if error?._code == 409 {
@@ -270,6 +274,8 @@ class OSUserExecutor {
270274

271275
OneSignalClient.shared().execute(request) { response in
272276
if let response = response {
277+
// Clear local data in preparation for hydration
278+
OneSignalUserManagerImpl.sharedInstance.clearUserData()
273279
parseFetchUserResponse(response: response, identityModel: request.identityModel, originalPushToken: OneSignalUserManagerImpl.sharedInstance.token)
274280
}
275281
} onFailure: { _ in
@@ -325,6 +331,11 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
325331
pushSubscriptionObject["token"] = pushSubscriptionModel.address
326332
pushSubscriptionObject["enabled"] = pushSubscriptionModel.enabled
327333

334+
pushSubscriptionObject["test_type"] = pushSubscriptionModel.testType
335+
pushSubscriptionObject["device_os"] = pushSubscriptionModel.deviceOs
336+
pushSubscriptionObject["sdk"] = pushSubscriptionModel.sdk
337+
pushSubscriptionObject["device_model"] = pushSubscriptionModel.deviceModel
338+
328339
// notificationTypes defaults to -1 instead of nil, don't send if it's -1
329340
if pushSubscriptionModel.notificationTypes != -1 {
330341
pushSubscriptionObject["notification_types"] = pushSubscriptionModel.notificationTypes

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
283283
)
284284
}
285285

286+
/**
287+
Clears the existing user's data in preparation for hydration via a fetch user call.
288+
*/
289+
func clearUserData() {
290+
// Identity and property models should still be the same instances, but with data cleared
291+
_user?.identityModel.clearData()
292+
_user?.propertiesModel.clearData()
293+
294+
// Subscription model store should be cleared completely
295+
OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.clearModelsFromStore()
296+
}
297+
286298
private func _login(externalId: String?, token: String?) -> OSUserInternal {
287299
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
288300
return _mockUser

0 commit comments

Comments
 (0)