From 73410f64b5ce423df256ae3a339d809f12d97c89 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 31 May 2017 12:14:18 -0700 Subject: [PATCH] Display a notification when unable retrieve messages for push Fixes #6684 // FREEBIE --- res/values/strings.xml | 2 ++ .../jobs/PushNotificationReceiveJob.java | 2 ++ .../notifications/MessageNotifier.java | 13 ++++++- .../PendingMessageNotificationBuilder.java | 35 +++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/org/thoughtcrime/securesms/notifications/PendingMessageNotificationBuilder.java diff --git a/res/values/strings.xml b/res/values/strings.xml index ba85ca42d5..3fab75e9f8 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -670,6 +670,8 @@ Mark read Media message Reply + Pending Signal messages + You have pending Signal messages, tap to open and retrieve Manual MMS settings are required for your phone. diff --git a/src/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java b/src/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java index fde0478a39..5a96652c94 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java @@ -4,6 +4,7 @@ import android.content.Context; import android.util.Log; import org.thoughtcrime.securesms.dependencies.InjectableType; +import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.whispersystems.jobqueue.JobParameters; import org.whispersystems.jobqueue.requirements.NetworkRequirement; import org.whispersystems.signalservice.api.SignalServiceMessageReceiver; @@ -50,5 +51,6 @@ public class PushNotificationReceiveJob extends PushReceivedJob implements Injec @Override public void onCanceled() { Log.w(TAG, "***** Failed to download pending message!"); + MessageNotifier.notifyMessagesPending(getContext()); } } diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 3e3085c44d..ad98e2d2ea 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -84,6 +84,7 @@ public class MessageNotifier { public static final String EXTRA_REMOTE_REPLY = "extra_remote_reply"; private static final int SUMMARY_NOTIFICATION_ID = 1338; + private static final int PENDING_MESSAGES_ID = 1111; private static final String NOTIFICATION_GROUP = "messages"; private static final long MIN_AUDIBLE_PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(2); private static final long DESKTOP_ACTIVITY_PERIOD = TimeUnit.MINUTES.toMillis(1); @@ -116,6 +117,15 @@ public class MessageNotifier { } } + public static void notifyMessagesPending(Context context) { + if (!TextSecurePreferences.isNotificationsEnabled(context)) { + return; + } + + PendingMessageNotificationBuilder builder = new PendingMessageNotificationBuilder(context, TextSecurePreferences.getNotificationPrivacy(context)); + ServiceUtil.getNotificationManager(context).notify(PENDING_MESSAGES_ID, builder.build()); + } + public static void cancelDelayedNotifications() { executor.cancel(); } @@ -153,7 +163,8 @@ public class MessageNotifier { if (notification.getId() != SUMMARY_NOTIFICATION_ID && notification.getId() != CallNotificationBuilder.WEBRTC_NOTIFICATION && notification.getId() != KeyCachingService.SERVICE_RUNNING_ID && - notification.getId() != MessageRetrievalService.FOREGROUND_ID) + notification.getId() != MessageRetrievalService.FOREGROUND_ID && + notification.getId() != PENDING_MESSAGES_ID) { for (NotificationItem item : notificationState.getNotifications()) { if (notification.getId() == (SUMMARY_NOTIFICATION_ID + item.getThreadId())) { diff --git a/src/org/thoughtcrime/securesms/notifications/PendingMessageNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/PendingMessageNotificationBuilder.java new file mode 100644 index 0000000000..3874beb9b9 --- /dev/null +++ b/src/org/thoughtcrime/securesms/notifications/PendingMessageNotificationBuilder.java @@ -0,0 +1,35 @@ +package org.thoughtcrime.securesms.notifications; + + +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; + +import org.thoughtcrime.securesms.ConversationListActivity; +import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase; +import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference; +import org.thoughtcrime.securesms.util.TextSecurePreferences; + +public class PendingMessageNotificationBuilder extends AbstractNotificationBuilder { + + public PendingMessageNotificationBuilder(Context context, NotificationPrivacyPreference privacy) { + super(context, privacy); + + Intent intent = new Intent(context, ConversationListActivity.class); + + setSmallIcon(R.drawable.icon_notification); + setColor(context.getResources().getColor(R.color.textsecure_primary)); + setPriority(TextSecurePreferences.getNotificationPriority(context)); + setCategory(NotificationCompat.CATEGORY_MESSAGE); + + setContentTitle(context.getString(R.string.MessageNotifier_pending_signal_messages)); + setContentText(context.getString(R.string.MessageNotifier_you_have_pending_signal_messages)); + setTicker(context.getString(R.string.MessageNotifier_you_have_pending_signal_messages)); + + setContentIntent(PendingIntent.getActivity(context, 0, intent, 0)); + setAutoCancel(true); + setAlarms(null, RecipientPreferenceDatabase.VibrateState.DEFAULT); + } +}