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

View File

@ -47,10 +47,10 @@ object LokiPushNotificationManager {
} }
@JvmStatic @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 oldToken = TextSecurePreferences.getFCMToken(context)
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(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 parameters = mapOf( "token" to token, "pubKey" to hexEncodedPublicKey )
val url = "${server}/register" val url = "${server}/register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))

View File

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

View File

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

View File

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

View File

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