Skip to content

Commit bafe588

Browse files
author
Alexis Oyama
committed
perf(ActionManager): Send push token in start and when token changed
SDK will no longer have data loss so it is not needed to send on every launch.
1 parent 2d71712 commit bafe588

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

Leanplum-SDK/Classes/LPActionManager.m

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ LeanplumMessageMatchResult LeanplumMessageMatchResultMake(BOOL matchedTrigger, B
4848

4949
@implementation NSObject (LeanplumExtension)
5050

51-
- (NSString *)leanplum_createTokenKey
52-
{
53-
return [NSString stringWithFormat:
54-
LEANPLUM_DEFAULTS_PUSH_TOKEN_KEY,
55-
LeanplumRequest.appId, LeanplumRequest.userId, LeanplumRequest.deviceId];
56-
}
57-
5851
- (void)leanplum_disableAskToAsk
5952
{
6053
#if LP_NOT_TV
@@ -77,26 +70,24 @@ - (void)leanplum_application:(UIApplication *)application didRegisterForRemoteNo
7770
[self leanplum_disableAskToAsk];
7871
}
7972

80-
// Send push token on every launch. This is required because push token may not be sent if the
81-
// user force quit the app. This is due to how LPRequestStorage keeps the data in memory and
82-
// we don't support response block on batch.
73+
// Format push token.
8374
NSString *formattedToken = [deviceToken description];
8475
formattedToken = [[[formattedToken stringByReplacingOccurrencesOfString:@"<" withString:@""]
8576
stringByReplacingOccurrencesOfString:@">" withString:@""]
8677
stringByReplacingOccurrencesOfString:@" " withString:@""];
87-
[Leanplum onStartResponse:^(BOOL success) {
88-
LP_END_USER_CODE
89-
[[LeanplumRequest post:LP_METHOD_SET_DEVICE_ATTRIBUTES
90-
params:@{LP_PARAM_DEVICE_PUSH_TOKEN: formattedToken}] send];
91-
LP_BEGIN_USER_CODE
92-
}];
9378

94-
// Update push token if it's different.
95-
NSString *tokenKey = [self leanplum_createTokenKey];
79+
// Send push token if we don't have one and when the token changed.
80+
// We no longer send in start's response because saved push token will be send in start too.
81+
NSString *tokenKey = [Leanplum pushTokenKey];
9682
NSString *existingToken = [[NSUserDefaults standardUserDefaults] stringForKey:tokenKey];
97-
if (![existingToken isEqualToString: formattedToken]) {
98-
[[NSUserDefaults standardUserDefaults] setObject: formattedToken forKey:tokenKey];
83+
if (!existingToken || ![existingToken isEqualToString:formattedToken]) {
84+
[[NSUserDefaults standardUserDefaults] setObject:formattedToken forKey:tokenKey];
9985
[[NSUserDefaults standardUserDefaults] synchronize];
86+
87+
if ([Leanplum hasStarted]) {
88+
[[LeanplumRequest post:LP_METHOD_SET_DEVICE_ATTRIBUTES
89+
params:@{LP_PARAM_DEVICE_PUSH_TOKEN: formattedToken}] send];
90+
}
10091
}
10192
LP_END_TRY
10293

@@ -140,7 +131,7 @@ - (void)leanplum_application:(UIApplication *)application didFailToRegisterForRe
140131
{
141132
LP_TRY
142133
[self leanplum_disableAskToAsk];
143-
NSString *tokenKey = [self leanplum_createTokenKey];
134+
NSString *tokenKey = [Leanplum pushTokenKey];
144135
if ([[NSUserDefaults standardUserDefaults] stringForKey:tokenKey]) {
145136
[[NSUserDefaults standardUserDefaults] removeObjectForKey:tokenKey];
146137
[[NSUserDefaults standardUserDefaults] synchronize];
@@ -322,7 +313,7 @@ - (void)sendUserNotificationSettingsIfChanged:(UIUserNotificationSettings *)noti
322313
if (![existingSettings isEqualToDictionary:settings]) {
323314
[[NSUserDefaults standardUserDefaults] setObject:settings forKey:settingsKey];
324315
[[NSUserDefaults standardUserDefaults] synchronize];
325-
NSString *tokenKey = [self leanplum_createTokenKey];
316+
NSString *tokenKey = [Leanplum pushTokenKey];
326317
NSString *existingToken = [[NSUserDefaults standardUserDefaults] stringForKey:tokenKey];
327318
NSMutableDictionary *params = [@{
328319
LP_PARAM_DEVICE_USER_NOTIFICATION_TYPES: types,

Leanplum-SDK/Classes/Leanplum.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ + (void)synchronizeDefaults
424424
});
425425
}
426426

427+
+ (NSString *)pushTokenKey
428+
{
429+
return [NSString stringWithFormat: LEANPLUM_DEFAULTS_PUSH_TOKEN_KEY,
430+
LeanplumRequest.appId, LeanplumRequest.userId, LeanplumRequest.deviceId];
431+
}
432+
427433
+ (void)start
428434
{
429435
[self startWithUserId:nil userAttributes:nil responseHandler:nil];
@@ -878,6 +884,14 @@ + (void)startWithUserId:(NSString *)userId
878884

879885
// Get the current Inbox messages on the device.
880886
params[LP_PARAM_INBOX_MESSAGES] = [self.inbox messagesIds];
887+
888+
// Push token.
889+
NSString *pushTokenKey = [Leanplum pushTokenKey];
890+
NSString *pushToken = [[NSUserDefaults standardUserDefaults] stringForKey:pushTokenKey];
891+
if (pushToken) {
892+
params[LP_PARAM_DEVICE_PUSH_TOKEN] = pushToken;
893+
}
894+
params[LP_PARAM_DEVICE_PUSH_TOKEN] = @"123";
881895

882896
// Issue start API call.
883897
LeanplumRequest *req = [LeanplumRequest post:LP_METHOD_START params:params];

Leanplum-SDK/Classes/LeanplumInternal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ andParameters:(NSDictionary *)params;
136136
+ (void)onEventsChanged:(LeanplumEventsChangedBlock)block;
137137
+ (void)synchronizeDefaults;
138138

139+
/**
140+
* Returns a push token using app ID, device ID, and user ID.
141+
*/
142+
+ (NSString *)pushTokenKey;
143+
139144
void LPLog(LPLogType type, NSString* format, ...);
140145

141146
@end

0 commit comments

Comments
 (0)