Skip to content

Commit

Permalink
Cache device token for faster loading on app opens
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulrvp committed Aug 29, 2024
1 parent 86b3797 commit 418560c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
19 changes: 19 additions & 0 deletions android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,32 @@ 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";
private static final String PREF_FILE_EMAIL = "BsftEmailPrefFile";

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String>() {
@Override
public void onSuccess(String token) {
BlueShiftPreference.saveDeviceToken(context, token);
setFirebaseToken(token);
}
})
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ protected void onMessageNotFound(Map<String, String> 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.
Expand Down

0 comments on commit 418560c

Please sign in to comment.