Skip to content

Commit 7e40489

Browse files
committed
Add migration when application updated but not launched yet
* If the application was upgraded but not launched and then a notification arrives, OneSignalNotificationServiceExtensionHandler, onNotificationReceived will fail due to accessing saved notifications ids without migration
1 parent ec15fa8 commit 7e40489

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

iOS_SDK/OneSignalSDK/Source/OSMigrationController.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,27 @@ - (void)migrate {
5454
- (void)migrateToVersion_02_14_00_AndGreater {
5555
let influenceVersion = 21400;
5656
let uniqueCacheOutcomeVersion = 21403;
57-
let sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
57+
long sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
5858
if (sdkVersion < influenceVersion) {
59-
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSIndirectNotification from version: %d", sdkVersion]];
59+
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSIndirectNotification from version: %ld", sdkVersion]];
6060

6161
[NSKeyedUnarchiver setClass:[OSIndirectInfluence class] forClassName:@"OSIndirectNotification"];
6262
NSArray<OSIndirectInfluence *> * indirectInfluenceData = [[OneSignal influenceDataRepository] lastNotificationsReceivedData];
63-
if (indirectInfluenceData)
63+
if (indirectInfluenceData) {
64+
[NSKeyedArchiver setClassName:@"OSIndirectInfluence" forClass:[OSIndirectInfluence class]];
6465
[[OneSignal influenceDataRepository] saveNotifications:indirectInfluenceData];
66+
}
6567
}
6668

6769
if (sdkVersion < uniqueCacheOutcomeVersion) {
68-
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSUniqueOutcomeNotification from version: %d", sdkVersion]];
70+
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSUniqueOutcomeNotification from version: %ld", sdkVersion]];
6971

7072
[NSKeyedUnarchiver setClass:[OSCachedUniqueOutcome class] forClassName:@"OSUniqueOutcomeNotification"];
7173
NSArray<OSCachedUniqueOutcome *> * attributedCacheUniqueOutcomeEvents = [[OneSignal outcomeEventsCache] getAttributedUniqueOutcomeEventSent];
72-
if (attributedCacheUniqueOutcomeEvents)
74+
if (attributedCacheUniqueOutcomeEvents) {
75+
[NSKeyedArchiver setClassName:@"OSCachedUniqueOutcome" forClass:[OSCachedUniqueOutcome class]];
7376
[[OneSignal outcomeEventsCache] saveAttributedUniqueOutcomeEventNotificationIds:attributedCacheUniqueOutcomeEvents];
77+
}
7478
}
7579
}
7680

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ + (void)updateNotificationTypes:(int)notificationTypes {
21952195
return;
21962196

21972197
if (!startedRegister && [self shouldRegisterNow])
2198-
[OneSignal registerUser];
2198+
[self registerUser];
21992199
else
22002200
[self sendNotificationTypesUpdate];
22012201

iOS_SDK/OneSignalSDK/Source/OneSignalNotificationServiceExtensionHandler.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "OneSignalInternal.h"
3636
#import "OneSignalReceiveReceiptsController.h"
3737
#import "OSSessionManager.h"
38+
#import "OSMigrationController.h"
3839

3940
@implementation OneSignalNotificationServiceExtensionHandler
4041

@@ -98,6 +99,8 @@ + (void)onNotificationReceived:(NSString *)receivedNotificationId {
9899
if (receivedNotificationId && ![receivedNotificationId isEqualToString:@""]) {
99100
// Track confirmed delivery
100101
[OneSignal.receiveReceiptsController sendReceiveReceiptWithNotificationId:receivedNotificationId];
102+
// If update was made without app being initialized/launched before -> migrate
103+
[[OSMigrationController new] migrate];
101104
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"NSE request received, sessionManager: %@", [OneSignal sessionManager]]];
102105
// Save received notification id
103106
[[OneSignal sessionManager] onNotificationReceived:receivedNotificationId];

iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ - (void)testIndirectNotificationToIndirectInfluenceMigration {
7777
NSMutableArray *indirectNotifications = [NSMutableArray new];
7878
[indirectNotifications addObject:indirectNotification];
7979

80+
[NSKeyedArchiver setClassName:@"OSIndirectNotification" forClass:[OSIndirectNotification class]];
8081
[dataRepository saveNotifications:indirectNotifications];
81-
NSArray *lastNotificationReceived = [dataRepository lastNotificationsReceivedData];
82+
[NSKeyedUnarchiver setClass:[OSIndirectNotification class] forClassName:@"OSIndirectNotification"];
83+
NSArray<OSIndirectNotification *> *lastNotificationReceived = [dataRepository lastNotificationsReceivedData];
84+
OSIndirectNotification *lastIndirectNotificationReceived = [lastNotificationReceived objectAtIndex:0];
8285
NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
86+
XCTAssertTrue([lastIndirectNotificationReceived.notificationId isEqualToString:notificationId]);
8387
XCTAssertEqual(1, [lastNotificationReceived count]);
8488
XCTAssertEqual(0, sdkVersion);
8589

@@ -125,6 +129,42 @@ - (void)testIndirectInfluenceToIndirectInfluenceMigration {
125129
XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration);
126130
}
127131

132+
- (void)testIndirectNotificationToIndirectInfluenceMigration_NotificationServiceExtensionHandler {
133+
NSString *notificationId = @"1234-5678-1234-5678-1234";
134+
double timestamp = 10;
135+
OSIndirectNotification *indirectNotification = [[OSIndirectNotification alloc] initWithParamsNotificationId:notificationId timestamp:timestamp];
136+
NSMutableArray *indirectNotifications = [NSMutableArray new];
137+
[indirectNotifications addObject:indirectNotification];
138+
139+
[NSKeyedArchiver setClassName:@"OSIndirectNotification" forClass:[OSIndirectNotification class]];
140+
[dataRepository saveNotifications:indirectNotifications];
141+
[NSKeyedUnarchiver setClass:[OSIndirectNotification class] forClassName:@"OSIndirectNotification"];
142+
NSArray<OSIndirectNotification *> *lastNotificationReceived = [dataRepository lastNotificationsReceivedData];
143+
OSIndirectNotification *lastIndirectNotificationReceived = [lastNotificationReceived objectAtIndex:0];
144+
NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
145+
XCTAssertTrue([lastIndirectNotificationReceived.notificationId isEqualToString:notificationId]);
146+
XCTAssertEqual(1, [lastNotificationReceived count]);
147+
XCTAssertEqual(0, sdkVersion);
148+
149+
// Receive notification
150+
[UnitTestCommonMethods receiveNotification:@"test_notification_1" wasOpened:NO];
151+
152+
NSArray<OSIndirectInfluence *> *lastNotificationReceivedAfterMigration = [dataRepository lastNotificationsReceivedData];
153+
XCTAssertEqual(2, [lastNotificationReceivedAfterMigration count]);
154+
155+
OSIndirectInfluence *indirectInfluenceMigrated = [lastNotificationReceivedAfterMigration objectAtIndex:0];
156+
XCTAssertTrue([indirectInfluenceMigrated.influenceId isEqualToString:notificationId]);
157+
XCTAssertTrue([indirectInfluenceMigrated.channelIdTag isEqualToString:@"notification_id"]);
158+
XCTAssertEqual(timestamp, indirectInfluenceMigrated.timestamp);
159+
160+
OSIndirectInfluence *indirectInfluenceReceived = [lastNotificationReceivedAfterMigration objectAtIndex:1];
161+
XCTAssertTrue([indirectInfluenceReceived.influenceId isEqualToString:@"test_notification_1"]);
162+
XCTAssertTrue([indirectInfluenceReceived.channelIdTag isEqualToString:@"notification_id"]);
163+
164+
NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
165+
XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration);
166+
}
167+
128168
- (void)testNoAttributedUniqueOutcomeDataAvailableToMigrate {
129169
NSArray *uniqueOutcomes = [outcomesCache getAttributedUniqueOutcomeEventSent];
130170
NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];

0 commit comments

Comments
 (0)