Fix FCM token updating bug

This commit is contained in:
nielsandriesse 2020-04-17 13:26:11 +10:00
parent 9f1809abf4
commit 669e8b9f6b
6 changed files with 9 additions and 9 deletions

View File

@ -205,7 +205,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
// Loki - Set up public chat manager
lokiPublicChatManager = new LokiPublicChatManager(this);
updatePublicChatProfilePictureIfNeeded();
registerForFCMIfNeeded();
registerForFCMIfNeeded(false);
}
@Override
@ -462,7 +462,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
}, this);
}
public void registerForFCMIfNeeded() {
public void registerForFCMIfNeeded(Boolean force) {
Context context = this;
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
@ -473,7 +473,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context);
if (userHexEncodedPublicKey == null) return;
if (TextSecurePreferences.isUsingFCM(this)) {
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context);
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context, force);
} else {
LokiPushNotificationManager.unregister(token, context);
}

View File

@ -47,10 +47,10 @@ object LokiPushNotificationManager {
}
@JvmStatic
fun register(token: String, hexEncodedPublicKey: String, context: Context?) {
fun register(token: String, hexEncodedPublicKey: String, context: Context?, force: Boolean) {
val oldToken = TextSecurePreferences.getFCMToken(context)
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
if (token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
val parameters = mapOf( "token" to token, "pubKey" to hexEncodedPublicKey )
val url = "${server}/register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))

View File

@ -162,7 +162,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
bottomSheet.onConfirmTapped = { isUsingFCM ->
TextSecurePreferences.setHasSeenPNModeSheet(this, true)
TextSecurePreferences.setIsUsingFCM(this, isUsingFCM)
ApplicationContext.getInstance(this).registerForFCMIfNeeded()
ApplicationContext.getInstance(this).registerForFCMIfNeeded(true)
bottomSheet.dismiss()
}
bottomSheet.onSkipTapped = {

View File

@ -103,7 +103,7 @@ class PNModeActivity : BaseActionBarActivity() {
val servers = DatabaseFactory.getLokiThreadDatabase(this).getAllPublicChatServers()
servers.forEach { publicChatAPI.setDisplayName(displayName, it) }
}
application.registerForFCMIfNeeded()
application.registerForFCMIfNeeded(true)
val intent = Intent(this, HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
show(intent)

View File

@ -39,7 +39,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
this.findPreference(fcmKey)
.setOnPreferenceChangeListener((preference, newValue) -> {
TextSecurePreferences.setIsUsingFCM(getContext(), (boolean) newValue);
ApplicationContext.getInstance(getContext()).registerForFCMIfNeeded();
ApplicationContext.getInstance(getContext()).registerForFCMIfNeeded(true);
return true;
});

View File

@ -16,7 +16,7 @@ class PushNotificationService : FirebaseMessagingService() {
super.onNewToken(token)
Log.d("Loki", "New FCM token: $token.")
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) ?: return
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this)
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this, false)
}
override fun onMessageReceived(message: RemoteMessage) {