mirror of
https://github.com/oxen-io/session-android.git
synced 2025-05-01 13:11:04 +00:00
Reinstate push v1
This commit is contained in:
parent
288b70bb14
commit
153aa4ceaa
@ -218,10 +218,6 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
|
|||||||
broadcaster = new Broadcaster(this);
|
broadcaster = new Broadcaster(this);
|
||||||
LokiAPIDatabase apiDB = getDatabaseComponent().lokiAPIDatabase();
|
LokiAPIDatabase apiDB = getDatabaseComponent().lokiAPIDatabase();
|
||||||
SnodeModule.Companion.configure(apiDB, broadcaster);
|
SnodeModule.Companion.configure(apiDB, broadcaster);
|
||||||
String userPublicKey = TextSecurePreferences.getLocalNumber(this);
|
|
||||||
if (userPublicKey != null) {
|
|
||||||
registerForPnIfNeeded(false);
|
|
||||||
}
|
|
||||||
initializeExpiringMessageManager();
|
initializeExpiringMessageManager();
|
||||||
initializeTypingStatusRepository();
|
initializeTypingStatusRepository();
|
||||||
initializeTypingStatusSender();
|
initializeTypingStatusSender();
|
||||||
@ -512,10 +508,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void clearAllData(boolean isMigratingToV2KeyPair) {
|
public void clearAllData(boolean isMigratingToV2KeyPair) {
|
||||||
String token = TextSecurePreferences.getFCMToken(this);
|
PushNotificationAPI.unregister();
|
||||||
if (token != null && !token.isEmpty()) {
|
|
||||||
PushNotificationAPI.unregister(token);
|
|
||||||
}
|
|
||||||
if (firebaseInstanceIdJob != null && firebaseInstanceIdJob.isActive()) {
|
if (firebaseInstanceIdJob != null && firebaseInstanceIdJob.isActive()) {
|
||||||
firebaseInstanceIdJob.cancel(null);
|
firebaseInstanceIdJob.cancel(null);
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
|||||||
if (response.success == true) {
|
if (response.success == true) {
|
||||||
Log.d(TAG, "Unsubscribe FCM success")
|
Log.d(TAG, "Unsubscribe FCM success")
|
||||||
TextSecurePreferences.setFCMToken(context, null)
|
TextSecurePreferences.setFCMToken(context, null)
|
||||||
PushNotificationAPI.unregister(token)
|
PushNotificationAPI.unregister()
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "Couldn't unregister for FCM due to error: ${response.message}")
|
Log.e(TAG, "Couldn't unregister for FCM due to error: ${response.message}")
|
||||||
}
|
}
|
||||||
@ -161,7 +161,10 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
|||||||
|
|
||||||
val oldToken = TextSecurePreferences.getFCMToken(context)
|
val oldToken = TextSecurePreferences.getFCMToken(context)
|
||||||
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
||||||
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) return
|
// if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) {
|
||||||
|
// Log.d(TAG, "not registering now... not forced or expired")
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
val pnKey = getOrCreateNotificationKey()
|
val pnKey = getOrCreateNotificationKey()
|
||||||
|
|
||||||
|
@ -24,7 +24,9 @@ class PushNotificationService : FirebaseMessagingService() {
|
|||||||
super.onNewToken(token)
|
super.onNewToken(token)
|
||||||
Log.d("Loki", "New FCM token: $token.")
|
Log.d("Loki", "New FCM token: $token.")
|
||||||
TextSecurePreferences.getLocalNumber(this) ?: return
|
TextSecurePreferences.getLocalNumber(this) ?: return
|
||||||
pushManager.refresh(true)
|
if (TextSecurePreferences.getLocalNumber(this) != token) {
|
||||||
|
pushManager.refresh(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMessageReceived(message: RemoteMessage) {
|
override fun onMessageReceived(message: RemoteMessage) {
|
||||||
|
@ -29,25 +29,43 @@ object PushNotificationAPI {
|
|||||||
Unsubscribe("unsubscribe_closed_group");
|
Unsubscribe("unsubscribe_closed_group");
|
||||||
}
|
}
|
||||||
|
|
||||||
fun register(token: String? = TextSecurePreferences.getFCMToken(context)) {
|
fun register(token: String? = TextSecurePreferences.getLegacyFCMToken(context)) {
|
||||||
Log.d(TAG, "register: $token")
|
Log.d(TAG, "register: $token")
|
||||||
|
|
||||||
unregisterV1IfRequired()
|
register(token, TextSecurePreferences.getLocalNumber(context))
|
||||||
subscribeGroups()
|
subscribeGroups()
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
fun register(token: String?, publicKey: String?) {
|
||||||
fun unregister(token: String) {
|
Log.d(TAG, "register($token)")
|
||||||
Log.d(TAG, "unregister: $token")
|
|
||||||
|
|
||||||
unregisterV1IfRequired()
|
token ?: return
|
||||||
unsubscribeGroups()
|
publicKey ?: return
|
||||||
|
val parameters = mapOf("token" to token, "pubKey" to publicKey)
|
||||||
|
val url = "$legacyServer/register"
|
||||||
|
val body =
|
||||||
|
RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||||
|
val request = Request.Builder().url(url).post(body)
|
||||||
|
retryIfNeeded(maxRetryCount) {
|
||||||
|
OnionRequestAPI.sendOnionRequest(request.build(), legacyServer, legacyServerPublicKey, Version.V2)
|
||||||
|
.map { response ->
|
||||||
|
val code = response.info["code"] as? Int
|
||||||
|
if (code != null && code != 0) {
|
||||||
|
TextSecurePreferences.setLegacyFCMToken(context, token)
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Couldn't register for FCM due to error: ${response.info["message"] as? String ?: "null"}.")
|
||||||
|
}
|
||||||
|
}.fail { exception ->
|
||||||
|
Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
|
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
|
||||||
*/
|
*/
|
||||||
private fun unregisterV1IfRequired() {
|
@JvmStatic
|
||||||
|
fun unregister() {
|
||||||
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: return
|
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: return
|
||||||
|
|
||||||
val parameters = mapOf( "token" to token )
|
val parameters = mapOf( "token" to token )
|
||||||
@ -56,17 +74,17 @@ object PushNotificationAPI {
|
|||||||
val request = Request.Builder().url(url).post(body).build()
|
val request = Request.Builder().url(url).post(body).build()
|
||||||
retryIfNeeded(maxRetryCount) {
|
retryIfNeeded(maxRetryCount) {
|
||||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||||
|
TextSecurePreferences.clearLegacyFCMToken(context)
|
||||||
when (response.info["code"]) {
|
when (response.info["code"]) {
|
||||||
null, 0 -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${response.info["message"]}.")
|
null, 0 -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${response.info["message"]}.")
|
||||||
else -> {
|
else -> Log.d(TAG, "unregisterV1 success token: $token")
|
||||||
TextSecurePreferences.clearLegacyFCMToken(context)
|
|
||||||
Log.d(TAG, "unregisterV1 success token: $token")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}.fail { exception ->
|
}.fail { exception ->
|
||||||
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsubscribeGroups()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy Closed Groups
|
// Legacy Closed Groups
|
||||||
@ -110,7 +128,7 @@ object PushNotificationAPI {
|
|||||||
else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}")
|
else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}")
|
||||||
}
|
}
|
||||||
}.fail { exception ->
|
}.fail { exception ->
|
||||||
Log.d(TAG, "Couldn't ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.")
|
Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -318,6 +318,11 @@ interface TextSecurePreferences {
|
|||||||
return getStringPreference(context, FCM_TOKEN_LEGACY, "")
|
return getStringPreference(context, FCM_TOKEN_LEGACY, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun setLegacyFCMToken(context: Context, token: String?) {
|
||||||
|
setStringPreference(context, FCM_TOKEN_LEGACY, token)
|
||||||
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun clearLegacyFCMToken(context: Context) {
|
fun clearLegacyFCMToken(context: Context) {
|
||||||
removePreference(context, FCM_TOKEN_LEGACY)
|
removePreference(context, FCM_TOKEN_LEGACY)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user