@@ -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,
0 commit comments