mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
Merge pull request #174 from loki-project/push-notifications
Improved Push Notifications Stage 2
This commit is contained in:
commit
e68c2d1cfe
@ -47,6 +47,7 @@
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
@ -512,6 +513,14 @@
|
||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
|
||||
android:exported="true"
|
||||
android:theme="@style/TextSecure.LightNoActionBar" />
|
||||
<service
|
||||
android:name="org.thoughtcrime.securesms.service.PushNotificationService"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="com.google.firebase.MESSAGING_EVENT" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
<service
|
||||
android:name="org.thoughtcrime.securesms.service.WebRtcCallService"
|
||||
android:enabled="true" />
|
||||
|
@ -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,6 +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:$google_services_version"
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +27,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 +91,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:$firebase_messaging_version"
|
||||
|
||||
implementation 'com.google.android.exoplayer:exoplayer-core:2.9.1'
|
||||
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.1'
|
||||
|
||||
|
@ -30,6 +30,8 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.multidex.MultiDexApplication;
|
||||
|
||||
import com.google.firebase.iid.FirebaseInstanceId;
|
||||
|
||||
import org.conscrypt.Conscrypt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.signal.aesgcmprovider.AesGcmProvider;
|
||||
@ -61,6 +63,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;
|
||||
@ -97,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;
|
||||
@ -182,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);
|
||||
@ -199,6 +205,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
// Loki - Set up public chat manager
|
||||
lokiPublicChatManager = new LokiPublicChatManager(this);
|
||||
updatePublicChatProfilePictureIfNeeded();
|
||||
setUpFCMIfNeeded();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -455,6 +462,19 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
}, this);
|
||||
}
|
||||
|
||||
public void setUpFCMIfNeeded() {
|
||||
Context context = this;
|
||||
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
|
||||
public void ping(@NotNull String s) {
|
||||
// TODO: Implement
|
||||
|
@ -0,0 +1,81 @@
|
||||
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 org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement
|
||||
import java.io.IOException
|
||||
|
||||
object LokiPushNotificationManager {
|
||||
private val connection = OkHttpClient()
|
||||
|
||||
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 {
|
||||
|
||||
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.setIsUsingFCM(context, false)
|
||||
} else {
|
||||
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 FCM.")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun register(token: String, hexEncodedPublicKey: String, context: Context?) {
|
||||
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 {
|
||||
|
||||
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.setIsUsingFCM(context, true)
|
||||
TextSecurePreferences.setFCMToken(context, token)
|
||||
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
||||
} else {
|
||||
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 for FCM.")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import nl.komponents.kovenant.functional.map
|
||||
import org.thoughtcrime.securesms.ApplicationContext
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
|
||||
import org.thoughtcrime.securesms.loki.LokiPushNotificationManager
|
||||
import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
|
||||
|
@ -0,0 +1,36 @@
|
||||
package org.thoughtcrime.securesms.service
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
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 FCM 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.")
|
||||
}
|
||||
}
|
||||
}
|
@ -185,6 +185,36 @@ public class TextSecurePreferences {
|
||||
|
||||
private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode";
|
||||
|
||||
// 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 isUsingFCM(Context context) {
|
||||
return getBooleanPreference(context, IS_USING_FCM, false);
|
||||
}
|
||||
|
||||
public static void setIsUsingFCM(Context context, boolean value) {
|
||||
setBooleanPreference(context, IS_USING_FCM, value);
|
||||
}
|
||||
|
||||
public static String getFCMToken(Context context) {
|
||||
return getStringPreference(context, FCM_TOKEN, "");
|
||||
}
|
||||
|
||||
public static void setFCMToken(Context context, String value) {
|
||||
setStringPreference(context, FCM_TOKEN, value);
|
||||
}
|
||||
|
||||
public static long getLastFCMUploadTime(Context context) {
|
||||
return getLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user