Add you may have messages notification.

This commit is contained in:
Clark
2023-07-19 16:05:00 -04:00
committed by Nicholas
parent 5242b9af39
commit 0fde404da8
7 changed files with 123 additions and 53 deletions

View File

@@ -1,13 +1,23 @@
package org.thoughtcrime.securesms.gcm
import android.app.Notification
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import org.signal.core.util.PendingIntentFlags.mutable
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.MainActivity
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob
import org.thoughtcrime.securesms.messages.WebSocketStrategy
import org.thoughtcrime.securesms.notifications.NotificationChannels
import org.thoughtcrime.securesms.notifications.NotificationIds
import org.thoughtcrime.securesms.util.FeatureFlags
import org.thoughtcrime.securesms.util.SignalLocalMetrics
import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor
import kotlin.time.Duration.Companion.minutes
@@ -38,6 +48,9 @@ object FcmFetchManager {
@Volatile
private var activeCount = 0
@Volatile
private var highPriority = false
/**
* @return True if a service was successfully started, otherwise false.
*/
@@ -56,13 +69,41 @@ object FcmFetchManager {
FcmFetchForegroundService.startServiceIfNecessary(context)
}
private fun postMayHaveMessagesNotification(context: Context) {
if (FeatureFlags.fcmMayHaveMessagesNotificationKillSwitch()) {
Log.w(TAG, "May have messages notification kill switch")
return
}
val mayHaveMessagesNotification: Notification = NotificationCompat.Builder(context, NotificationChannels.getInstance().ADDITIONAL_MESSAGE_NOTIFICATIONS)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.FcmFetchManager__you_may_have_messages))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(PendingIntent.getActivity(context, 0, MainActivity.clearTop(context), mutable()))
.setVibrate(longArrayOf(0))
.setOnlyAlertOnce(true)
.build()
NotificationManagerCompat.from(context)
.notify(NotificationIds.MAY_HAVE_MESSAGES_NOTIFICATION_ID, mayHaveMessagesNotification)
}
private fun cancelMayHaveMessagesNotification(context: Context) {
NotificationManagerCompat.from(context).cancel(NotificationIds.MAY_HAVE_MESSAGES_NOTIFICATION_ID)
}
private fun fetch(context: Context) {
val hasHighPriorityContext = highPriority
val metricId = SignalLocalMetrics.PushWebsocketFetch.startFetch()
val success = retrieveMessages(context)
if (!success) {
SignalLocalMetrics.PushWebsocketFetch.onTimedOut(metricId)
} else {
if (success) {
SignalLocalMetrics.PushWebsocketFetch.onDrained(metricId)
cancelMayHaveMessagesNotification(context)
} else {
SignalLocalMetrics.PushWebsocketFetch.onTimedOut(metricId)
if (hasHighPriorityContext) {
postMayHaveMessagesNotification(context)
}
}
synchronized(this) {
@@ -72,13 +113,22 @@ object FcmFetchManager {
Log.i(TAG, "No more active. Stopping.")
context.stopService(Intent(context, FcmFetchBackgroundService::class.java))
FcmFetchForegroundService.stopServiceIfNecessary(context)
highPriority = false
}
}
}
@JvmStatic
fun enqueueFetch(context: Context) {
fun onForeground(context: Context) {
cancelMayHaveMessagesNotification(context)
}
@JvmStatic
fun enqueueFetch(context: Context, highPriority: Boolean) {
synchronized(this) {
if (highPriority) {
this.highPriority = true
}
val performedReplace = EXECUTOR.enqueue { fetch(context) }
if (performedReplace) {
Log.i(TAG, "Already have one running and one enqueued. Ignoring.")

View File

@@ -74,9 +74,8 @@ public class FcmReceiveService extends FirebaseMessagingService {
}
private static void handleReceivedNotification(Context context, @Nullable RemoteMessage remoteMessage) {
boolean highPriority = remoteMessage != null && remoteMessage.getPriority() == RemoteMessage.PRIORITY_HIGH;
try {
boolean highPriority = remoteMessage != null && remoteMessage.getPriority() == RemoteMessage.PRIORITY_HIGH;
Log.d(TAG, String.format(Locale.US, "[handleReceivedNotification] API: %s, RemoteMessagePriority: %s", Build.VERSION.SDK_INT, remoteMessage != null ? remoteMessage.getPriority() : "n/a"));
if (highPriority) {
@@ -88,7 +87,7 @@ public class FcmReceiveService extends FirebaseMessagingService {
Log.w(TAG, "Failed to start service.", e);
}
FcmFetchManager.enqueueFetch(context);
FcmFetchManager.enqueueFetch(context, highPriority);
}
private static void handleRegistrationPushChallenge(@NonNull String challenge) {