mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-22 16:07:30 +00:00
add FCM to app for remote push notification
This commit is contained in:
parent
562deec218
commit
b72048d9a4
@ -47,6 +47,7 @@
|
|||||||
<uses-permission android:name="android.permission.VIBRATE" />
|
<uses-permission android:name="android.permission.VIBRATE" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<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.WAKE_LOCK" />
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
@ -509,6 +510,14 @@
|
|||||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
|
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:theme="@style/TextSecure.LightNoActionBar" />
|
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
|
<service
|
||||||
android:name="org.thoughtcrime.securesms.service.WebRtcCallService"
|
android:name="org.thoughtcrime.securesms.service.WebRtcCallService"
|
||||||
android:enabled="true" />
|
android:enabled="true" />
|
||||||
|
@ -16,6 +16,7 @@ buildscript {
|
|||||||
classpath "com.android.tools.build:gradle:$gradle_version"
|
classpath "com.android.tools.build:gradle:$gradle_version"
|
||||||
classpath files('libs/gradle-witness.jar')
|
classpath files('libs/gradle-witness.jar')
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
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: 'kotlin-android'
|
||||||
apply plugin: 'witness'
|
apply plugin: 'witness'
|
||||||
apply plugin: 'kotlin-kapt'
|
apply plugin: 'kotlin-kapt'
|
||||||
|
apply plugin: 'com.google.gms.google-services'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
@ -87,6 +89,8 @@ dependencies {
|
|||||||
implementation 'android.arch.lifecycle:extensions:1.1.1'
|
implementation 'android.arch.lifecycle:extensions:1.1.1'
|
||||||
implementation 'android.arch.lifecycle:common-java8: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-core:2.9.1'
|
||||||
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.1'
|
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.1'
|
||||||
|
|
||||||
|
@ -30,6 +30,11 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.multidex.MultiDexApplication;
|
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.conscrypt.Conscrypt;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.signal.aesgcmprovider.AesGcmProvider;
|
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.PersistentLogger;
|
||||||
import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger;
|
import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger;
|
||||||
import org.thoughtcrime.securesms.loki.LokiPublicChatManager;
|
import org.thoughtcrime.securesms.loki.LokiPublicChatManager;
|
||||||
|
import org.thoughtcrime.securesms.loki.LokiPushNotificationManager;
|
||||||
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
|
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
|
||||||
import org.thoughtcrime.securesms.loki.redesign.activities.HomeActivity;
|
import org.thoughtcrime.securesms.loki.redesign.activities.HomeActivity;
|
||||||
import org.thoughtcrime.securesms.loki.redesign.messaging.BackgroundOpenGroupPollWorker;
|
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
|
// Loki - Set up public chat manager
|
||||||
lokiPublicChatManager = new LokiPublicChatManager(this);
|
lokiPublicChatManager = new LokiPublicChatManager(this);
|
||||||
updatePublicChatProfilePictureIfNeeded();
|
updatePublicChatProfilePictureIfNeeded();
|
||||||
|
setUpFirebaseDeviceToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -453,6 +460,24 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
|||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUpFirebaseDeviceToken() {
|
||||||
|
Context context = this;
|
||||||
|
FirebaseInstanceId.getInstance().getInstanceId()
|
||||||
|
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
|
||||||
|
@Override
|
||||||
|
public void onComplete(@NonNull Task<InstanceIdResult> 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
|
@Override
|
||||||
public void ping(@NotNull String s) {
|
public void ping(@NotNull String s) {
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
|
@ -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}")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
|
|||||||
class BackgroundPollWorker : PersistentAlarmManagerListener() {
|
class BackgroundPollWorker : PersistentAlarmManagerListener() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val pollInterval = TimeUnit.MINUTES.toMillis(2)
|
private val pollInterval = TimeUnit.MINUTES.toMillis(20)
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun schedule(context: Context) {
|
fun schedule(context: Context) {
|
||||||
|
@ -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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -185,6 +185,16 @@ public class TextSecurePreferences {
|
|||||||
|
|
||||||
private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode";
|
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) {
|
public static boolean isScreenLockEnabled(@NonNull Context context) {
|
||||||
return getBooleanPreference(context, SCREEN_LOCK, false);
|
return getBooleanPreference(context, SCREEN_LOCK, false);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user