From 418560c92b427e97799ed386a7e9e53e565c3862 Mon Sep 17 00:00:00 2001 From: Rahul Raveendran Date: Thu, 29 Aug 2024 18:37:59 +0530 Subject: [PATCH] Cache device token for faster loading on app opens --- .../com/blueshift/BlueShiftPreference.java | 19 +++++++++++++++++ .../com/blueshift/BlueshiftAttributesApp.java | 21 ++++++++++++------- .../fcm/BlueshiftMessagingService.java | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java b/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java index 893436c3..4b5b2f3c 100644 --- a/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java +++ b/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java @@ -21,6 +21,7 @@ public class BlueShiftPreference { private static final String PREF_FILE = "com.blueshift.sdk_preferences"; private static final String PREF_KEY_APP_VERSION = "blueshift_app_version"; private static final String PREF_KEY_DEVICE_ID = "blueshift_device_id"; + private static final String PREF_KEY_DEVICE_TOKEN = "blueshift_device_token"; private static final String PREF_KEY_PUSH_ENABLED = "blueshift_push_enabled"; private static final String PREF_KEY_LEGACY_SYNC_COMPLETE = "blueshift_legacy_sync_complete"; private static final String PREF_KEY_APP_OPEN_TRACKED_AT = "blueshift_app_open_tracked_at"; @@ -28,6 +29,24 @@ public class BlueShiftPreference { private static final String TAG = "BlueShiftPreference"; + static String getSavedDeviceToken(Context context) { + String token = null; + + SharedPreferences preferences = getBlueshiftPreferences(context); + if (preferences != null) { + token = preferences.getString(PREF_KEY_DEVICE_TOKEN, token); + } + + return token; + } + + static void saveDeviceToken(Context context, String token) { + SharedPreferences preferences = getBlueshiftPreferences(context); + if (preferences != null) { + preferences.edit().putString(PREF_KEY_DEVICE_TOKEN, token).apply(); + } + } + static void markLegacyEventSyncAsComplete(Context context) { SharedPreferences preferences = getBlueshiftPreferences(context); if (preferences != null) { diff --git a/android-sdk/src/main/java/com/blueshift/BlueshiftAttributesApp.java b/android-sdk/src/main/java/com/blueshift/BlueshiftAttributesApp.java index 0d75abc9..0b467144 100644 --- a/android-sdk/src/main/java/com/blueshift/BlueshiftAttributesApp.java +++ b/android-sdk/src/main/java/com/blueshift/BlueshiftAttributesApp.java @@ -3,8 +3,6 @@ import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; import android.os.Build; @@ -102,7 +100,7 @@ public void init(Context context) { addInAppEnabledStatus(context); addPushEnabledStatus(context); addFirebaseInstanceId(context); - addFirebaseToken(context); + addDeviceToken(context); addDeviceId(context); addDeviceAdId(context); addDeviceLocation(context); @@ -373,30 +371,36 @@ private void setDeviceLocation(Location location) { } } - private void addFirebaseToken(Context context) { + private void addDeviceToken(Context context) { if (!BlueshiftUtils.isPushEnabled(context)) return; + String cachedToken = BlueShiftPreference.getSavedDeviceToken(context); + if (cachedToken != null) { + setFirebaseToken(cachedToken); + } + try { - addFirebaseToken(); + addFirebaseToken(context); } catch (Exception e) { // tickets#8919 reported an issue with fcm token fetch. this is the // fix for the same. we are manually calling initializeApp and trying // to get token again. FirebaseApp.initializeApp(context); try { - addFirebaseToken(); + addFirebaseToken(context); } catch (Exception e1) { BlueshiftLogger.e(TAG, e1); } } } - private void addFirebaseToken() { + private void addFirebaseToken(Context context) { try { FirebaseMessaging.getInstance().getToken() .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(String token) { + BlueShiftPreference.saveDeviceToken(context, token); setFirebaseToken(token); } }) @@ -480,8 +484,9 @@ private void setFirebaseInstanceId(String instanceId) { } } - public void updateFirebaseToken(String newToken) { + public void updateFirebaseToken(Context context, String newToken) { if (newToken != null) { + BlueShiftPreference.saveDeviceToken(context, newToken); setFirebaseToken(newToken); } } diff --git a/android-sdk/src/main/java/com/blueshift/fcm/BlueshiftMessagingService.java b/android-sdk/src/main/java/com/blueshift/fcm/BlueshiftMessagingService.java index f5fbc1c6..17de00c5 100644 --- a/android-sdk/src/main/java/com/blueshift/fcm/BlueshiftMessagingService.java +++ b/android-sdk/src/main/java/com/blueshift/fcm/BlueshiftMessagingService.java @@ -396,7 +396,7 @@ protected void onMessageNotFound(Map data) { public void onNewToken(@NonNull String newToken) { BlueshiftLogger.d(LOG_TAG, "onNewToken: " + newToken); - BlueshiftAttributesApp.getInstance().updateFirebaseToken(newToken); + BlueshiftAttributesApp.getInstance().updateFirebaseToken(this, newToken); // We are calling an identify here to make sure that the change in // device token is notified to the blueshift servers.