diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index bfa2c456dc..654d3bc721 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -47,6 +47,7 @@
+
@@ -509,6 +510,14 @@
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
android:exported="true"
android:theme="@style/TextSecure.LightNoActionBar" />
+
+
+
+
+
diff --git a/build.gradle b/build.gradle
index 41e2ac5d37..fef89e3a44 100644
--- a/build.gradle
+++ b/build.gradle
@@ -16,6 +16,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'
}
}
@@ -24,6 +25,7 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'witness'
apply plugin: 'kotlin-kapt'
+apply plugin: 'com.google.gms.google-services'
repositories {
mavenLocal()
@@ -87,6 +89,8 @@ 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.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 0687981f55..7e6215f2a7 100644
--- a/src/org/thoughtcrime/securesms/ApplicationContext.java
+++ b/src/org/thoughtcrime/securesms/ApplicationContext.java
@@ -30,6 +30,11 @@ 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;
import org.signal.aesgcmprovider.AesGcmProvider;
@@ -61,6 +66,7 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.logging.PersistentLogger;
import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger;
import org.thoughtcrime.securesms.loki.LokiPublicChatManager;
+import org.thoughtcrime.securesms.loki.LokiPushNotificationManager;
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
import org.thoughtcrime.securesms.loki.redesign.activities.HomeActivity;
import org.thoughtcrime.securesms.loki.redesign.messaging.BackgroundOpenGroupPollWorker;
@@ -197,6 +203,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
// Loki - Set up public chat manager
lokiPublicChatManager = new LokiPublicChatManager(this);
updatePublicChatProfilePictureIfNeeded();
+ setUpFirebaseDeviceToken();
}
@Override
@@ -453,6 +460,24 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
}, this);
}
+ public void setUpFirebaseDeviceToken() {
+ 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);
+ }
+ });
+ }
+
@Override
public void ping(@NotNull String s) {
// TODO: Implement
diff --git a/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt b/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt
new file mode 100644
index 0000000000..8378519412
--- /dev/null
+++ b/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt
@@ -0,0 +1,98 @@
+package org.thoughtcrime.securesms.loki
+
+import android.content.Context
+import okhttp3.*
+import org.thoughtcrime.securesms.util.TextSecurePreferences
+import org.whispersystems.libsignal.logging.Log
+import org.whispersystems.signalservice.internal.util.JsonUtil
+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
+ private val connection = OkHttpClient()
+
+ fun disableRemoteNotification(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 {
+
+ override fun onResponse(call: Call, response: Response) {
+ when (response.code()) {
+ 200 -> {
+ val bodyAsString = response.body()!!.string()
+ val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
+ val code = json?.get("code") as? Int
+ if (code != null && code != 0) {
+ TextSecurePreferences.setIsUsingRemoteNotification(context, false)
+ } else {
+ Log.d("Loki", "Couldn't disable remote notification due to error: ${json?.get("message") as? String}.")
+ }
+ }
+ }
+ }
+
+ override fun onFailure(call: Call, exception: IOException) {
+ Log.d("Loki", "Couldn't disable remote notification.")
+ }
+ })
+ }
+
+ @JvmStatic
+ fun register(token: String, hexEncodedPublicKey: String, context: Context?) {
+ 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 {
+
+ override fun onResponse(call: Call, response: Response) {
+ when (response.code()) {
+ 200 -> {
+ val bodyAsString = response.body()!!.string()
+ val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
+ val code = json?.get("code") as? Int
+ if (code != null && code != 0) {
+ TextSecurePreferences.setIsUsingRemoteNotification(context, true)
+ } else {
+ Log.d("Loki", "Couldn't register device token due to error: ${json?.get("message") as? String}.")
+ }
+ }
+ }
+ }
+
+ override fun onFailure(call: Call, exception: IOException) {
+ Log.d("Loki", "Couldn't register device token.")
+ }
+ })
+ }
+
+ fun acknowledgeDeliveryForMessageWith(hash: String, expiration: Int, hexEncodedPublicKey: String, context: Context?) {
+ val parameters = mapOf("hash" to hash, "pubKey" to hexEncodedPublicKey, "expiration" to expiration)
+ val url = "${server}acknowledge_message_delivery"
+ 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 {
+
+ override fun onResponse(call: Call, response: Response) {
+ when (response.code()) {
+ 200 -> {
+ val bodyAsString = response.body()!!.string()
+ val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
+ val code = json?.get("code") as? Int
+ if (code == null || code == 0) {
+ Log.d("Loki", "Couldn't acknowledge the delivery for message due to error: ${json?.get("message") as? String}.")
+ }
+ }
+ }
+ }
+
+ override fun onFailure(call: Call, exception: IOException) {
+ Log.d("Loki", "Couldn't acknowledge the delivery for message with last hash: ${hash}")
+ }
+ })
+ }
+}
diff --git a/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt b/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt
index 4b8fdc9037..19c335f41a 100644
--- a/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt
+++ b/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt
@@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
class BackgroundPollWorker : PersistentAlarmManagerListener() {
companion object {
- private val pollInterval = TimeUnit.MINUTES.toMillis(2)
+ private val pollInterval = TimeUnit.MINUTES.toMillis(20)
@JvmStatic
fun schedule(context: Context) {
diff --git a/src/org/thoughtcrime/securesms/service/PushNotificationService.kt b/src/org/thoughtcrime/securesms/service/PushNotificationService.kt
new file mode 100644
index 0000000000..c877510f59
--- /dev/null
+++ b/src/org/thoughtcrime/securesms/service/PushNotificationService.kt
@@ -0,0 +1,39 @@
+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
+import org.whispersystems.libsignal.logging.Log
+import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
+import org.whispersystems.signalservice.internal.util.Base64
+import org.whispersystems.signalservice.loki.messaging.LokiMessageWrapper
+
+class PushNotificationService: FirebaseMessagingService() {
+
+ override fun onNewToken(token: String) {
+ super.onNewToken(token)
+ Log.d("Loki", "new token ${token}")
+ val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
+ LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this)
+ }
+
+ override fun onMessageReceived(message: RemoteMessage) {
+ val base64EncodedData = message.data["ENCRYPTED_DATA"]
+ val data = base64EncodedData?.let { Base64.decode(it) }
+ if (data != null) {
+ try {
+ val envelope = LokiMessageWrapper.unwrap(data)
+ PushContentReceiveJob(this).processEnvelope(SignalServiceEnvelope(envelope))
+ } catch (e: Exception) {
+ Log.d("Loki", "Failed to unwrap data for message.")
+ }
+ } else {
+ 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 42c204dfbb..62fc6c258e 100644
--- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java
+++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java
@@ -185,6 +185,16 @@ public class TextSecurePreferences {
private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode";
+ private static final String IS_USING_REMOTE_NOTIFICATION = "pref_is_using_remote_notification";
+
+ public static boolean isUsingRemoteNotification(Context context) {
+ return getBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, false);
+ }
+
+ public static void setIsUsingRemoteNotification(Context context, boolean value) {
+ setBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, value);
+ }
+
public static boolean isScreenLockEnabled(@NonNull Context context) {
return getBooleanPreference(context, SCREEN_LOCK, false);
}