diff --git a/build.gradle b/build.gradle index fef89e3a44..57766bdf94 100644 --- a/build.gradle +++ b/build.gradle @@ -6,6 +6,8 @@ buildscript { ext.kovenant_version = "3.3.0" ext.identicon_version = "v11" ext.rss_parser_version = "2.0.4" + ext.google_services_version = "4.3.3" + ext.firebase_messaging_version = "18.0.0" repositories { mavenLocal() @@ -16,7 +18,7 @@ buildscript { classpath "com.android.tools.build:gradle:$gradle_version" classpath files('libs/gradle-witness.jar') classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.google.gms:google-services:4.3.3' + classpath "com.google.gms:google-services:$google_services_version" } } @@ -89,7 +91,7 @@ dependencies { implementation 'android.arch.lifecycle:extensions:1.1.1' implementation 'android.arch.lifecycle:common-java8:1.1.1' - implementation 'com.google.firebase:firebase-messaging:18.0.0' + implementation "com.google.firebase:firebase-messaging:$firebase_messaging_version" implementation 'com.google.android.exoplayer:exoplayer-core:2.9.1' implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.1' diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 364b30e570..9e1b633c5e 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -30,10 +30,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.multidex.MultiDexApplication; -import com.google.android.gms.tasks.OnCompleteListener; -import com.google.android.gms.tasks.Task; import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.iid.InstanceIdResult; import org.conscrypt.Conscrypt; import org.jetbrains.annotations.NotNull; @@ -103,6 +100,7 @@ import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol; import org.whispersystems.signalservice.loki.api.LokiP2PAPI; import org.whispersystems.signalservice.loki.api.LokiP2PAPIDelegate; import org.whispersystems.signalservice.loki.api.LokiPoller; +import org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement; import org.whispersystems.signalservice.loki.api.LokiSwarmAPI; import org.whispersystems.signalservice.loki.api.fileserver.LokiFileServerAPI; import org.whispersystems.signalservice.loki.api.publicchats.LokiPublicChat; @@ -188,6 +186,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc ProcessLifecycleOwner.get().getLifecycle().addObserver(this); // Loki - Set up P2P API if needed setUpP2PAPI(); + // Loki - Set up push notification acknowledgement + LokiPushNotificationAcknowledgement.Companion.configureIfNeeded(BuildConfig.DEBUG); // Loki - Update device mappings if (setUpStorageAPIIfNeeded()) { String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this); @@ -205,7 +205,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc // Loki - Set up public chat manager lokiPublicChatManager = new LokiPublicChatManager(this); updatePublicChatProfilePictureIfNeeded(); - setUpFirebaseDeviceToken(); + setUpFCMIfNeeded(); } @Override @@ -462,22 +462,17 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc }, this); } - public void setUpFirebaseDeviceToken() { + public void setUpFCMIfNeeded() { Context context = this; - FirebaseInstanceId.getInstance().getInstanceId() - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (!task.isSuccessful()) { - Log.w(TAG, "getInstanceId failed", task.getException()); - return; - } - // Get new Instance ID token - String token = task.getResult().getToken(); - String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context); - LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context); - } - }); + FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> { + if (!task.isSuccessful()) { + Log.w(TAG, "getInstanceId failed", task.getException()); + return; + } + String token = task.getResult().getToken(); + String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context); + LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context); + }); } @Override diff --git a/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt b/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt index 4858e2c818..336c9f8484 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt @@ -5,17 +5,21 @@ import okhttp3.* import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.libsignal.logging.Log import org.whispersystems.signalservice.internal.util.JsonUtil +import org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement import java.io.IOException object LokiPushNotificationManager { - //const val server = "https://live.apns.getsession.org/" - const val server = "https://dev.apns.getsession.org/" - const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000 private val connection = OkHttpClient() - fun disableRemoteNotification(token: String, context: Context?) { - val parameters = mapOf("token" to token) - val url = "${server}register" + private val server by lazy { + LokiPushNotificationAcknowledgement.shared.server + } + + private const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000 + + fun disableFCM(token: String, context: Context?) { + val parameters = mapOf( "token" to token ) + val url = "${server}/register" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body).build() connection.newCall(request).enqueue(object : Callback { @@ -27,28 +31,27 @@ object LokiPushNotificationManager { val json = JsonUtil.fromJson(bodyAsString, Map::class.java) val code = json?.get("code") as? Int if (code != null && code != 0) { - TextSecurePreferences.setIsUsingRemoteNotification(context, false) + TextSecurePreferences.setIsUsingFCM(context, false) } else { - Log.d("Loki", "Couldn't disable remote notification due to error: ${json?.get("message") as? String}.") + Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.") } } } } override fun onFailure(call: Call, exception: IOException) { - Log.d("Loki", "Couldn't disable remote notification.") + Log.d("Loki", "Couldn't disable FCM.") } }) } @JvmStatic fun register(token: String, hexEncodedPublicKey: String, context: Context?) { - if (token == TextSecurePreferences.getTokenForRemoteNotification(context) && System.currentTimeMillis() - TextSecurePreferences.getLastTimeForTokenUploading(context) < tokenExpirationInterval) { - return - } - - val parameters = mapOf("token" to token, "pubKey" to hexEncodedPublicKey) - val url = "${server}register" + val oldToken = TextSecurePreferences.getFCMToken(context) + val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context) + if (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)) val request = Request.Builder().url(url).post(body).build() connection.newCall(request).enqueue(object : Callback { @@ -60,21 +63,19 @@ object LokiPushNotificationManager { val json = JsonUtil.fromJson(bodyAsString, Map::class.java) val code = json?.get("code") as? Int if (code != null && code != 0) { - TextSecurePreferences.setIsUsingRemoteNotification(context, true) - TextSecurePreferences.setTokenForRemoteNotification(context, token) - TextSecurePreferences.setLastTimeForTokenUploading(context, System.currentTimeMillis()) + TextSecurePreferences.setIsUsingFCM(context, true) + TextSecurePreferences.setFCMToken(context, token) + TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis()) } else { - Log.d("Loki", "Couldn't register device token due to error: ${json?.get("message") as? String}.") + Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.") } } } } override fun onFailure(call: Call, exception: IOException) { - Log.d("Loki", "Couldn't register device token.") + Log.d("Loki", "Couldn't register for FCM.") } }) } - - } diff --git a/src/org/thoughtcrime/securesms/service/PushNotificationService.kt b/src/org/thoughtcrime/securesms/service/PushNotificationService.kt index c877510f59..362442e1a0 100644 --- a/src/org/thoughtcrime/securesms/service/PushNotificationService.kt +++ b/src/org/thoughtcrime/securesms/service/PushNotificationService.kt @@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.service import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage -import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.jobs.PushContentReceiveJob import org.thoughtcrime.securesms.loki.LokiPushNotificationManager import org.thoughtcrime.securesms.util.TextSecurePreferences @@ -11,11 +10,11 @@ import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope import org.whispersystems.signalservice.internal.util.Base64 import org.whispersystems.signalservice.loki.messaging.LokiMessageWrapper -class PushNotificationService: FirebaseMessagingService() { +class PushNotificationService : FirebaseMessagingService() { override fun onNewToken(token: String) { super.onNewToken(token) - Log.d("Loki", "new token ${token}") + Log.d("Loki", "New FCM token: $token.") val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this) } @@ -34,6 +33,4 @@ class PushNotificationService: FirebaseMessagingService() { Log.d("Loki", "Failed to decode data for message.") } } - - } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index c1fbd89b25..f4fc8b20ec 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -185,34 +185,35 @@ public class TextSecurePreferences { private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode"; - //Session for Push Notification - private static final String IS_USING_REMOTE_NOTIFICATION = "pref_is_using_remote_notification"; - private static final String TOKEN_FOR_REMOTE_NOTIFICATION = "pref_token_for_remote_notification"; - private static final String LAST_TIME_FOR_TOKEN_UPLOADING = "pref_last_time_for_token_uploading"; + // region FCM + private static final String IS_USING_FCM = "pref_is_using_fcm"; + private static final String FCM_TOKEN = "pref_fcm_token"; + private static final String LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time"; - public static boolean isUsingRemoteNotification(Context context) { - return getBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, false); + public static boolean isUsingFCM(Context context) { + return getBooleanPreference(context, IS_USING_FCM, false); } - public static void setIsUsingRemoteNotification(Context context, boolean value) { - setBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, value); + public static void setIsUsingFCM(Context context, boolean value) { + setBooleanPreference(context, IS_USING_FCM, value); } - public static String getTokenForRemoteNotification(Context context) { - return getStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, ""); + public static String getFCMToken(Context context) { + return getStringPreference(context, FCM_TOKEN, ""); } - public static void setTokenForRemoteNotification(Context context, String value) { - setStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, value); + public static void setFCMToken(Context context, String value) { + setStringPreference(context, FCM_TOKEN, value); } - public static long getLastTimeForTokenUploading(Context context) { - return getLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, 0); + public static long getLastFCMUploadTime(Context context) { + return getLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, 0); } - public static void setLastTimeForTokenUploading(Context context, long value) { - setLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, value); + public static void setLastFCMUploadTime(Context context, long value) { + setLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, value); } + // endregion public static boolean isScreenLockEnabled(@NonNull Context context) { return getBooleanPreference(context, SCREEN_LOCK, false);