diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 546f8d43d..fbfcdbbe9 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -302,14 +302,14 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity titleView.setTitle(glideRequests, dcChat); - dcContext.notificationManger.updateVisibleChat(chatId); + dcContext.notificationCenter.updateVisibleChat(chatId); } @Override protected void onPause() { super.onPause(); processComposeControls(ACTION_SAVE_DRAFT); - dcContext.notificationManger.updateVisibleChat(0); + dcContext.notificationCenter.updateVisibleChat(0); if (isFinishing()) overridePendingTransition(R.anim.fade_scale_in, R.anim.slide_to_right); quickAttachmentDrawer.onPause(); inputPanel.onPause(); @@ -1224,7 +1224,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity if (refreshFragment) { fragment.reload(recipient, chatId); - dcContext.notificationManger.updateVisibleChat(chatId); + dcContext.notificationCenter.updateVisibleChat(chatId); } fragment.scrollToBottom(); diff --git a/src/org/thoughtcrime/securesms/ConversationListFragment.java b/src/org/thoughtcrime/securesms/ConversationListFragment.java index e4a9275b9..5d0d1f1e4 100644 --- a/src/org/thoughtcrime/securesms/ConversationListFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationListFragment.java @@ -323,7 +323,7 @@ public class ConversationListFragment extends Fragment dcContext.marknoticedContact(getListAdapter().getDeaddropContactId()); } else { - dcContext.notificationManger.removeNotifications((int) chatId); + dcContext.notificationCenter.removeNotifications((int) chatId); dcContext.deleteChat((int) chatId); } } diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 171893842..e52af1e4a 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -53,7 +53,7 @@ public class ApplicationDcContext extends DcContext { public static final int RECIPIENT_TYPE_CONTACT = 1; public Context context; - public NotificationCenter notificationManger; + public NotificationCenter notificationCenter; public ApplicationDcContext(Context context) { super("Android "+BuildConfig.VERSION_NAME); @@ -122,7 +122,7 @@ public class ApplicationDcContext extends DcContext { Log.e(TAG, "Cannot create wakeLocks"); } - notificationManger = new NotificationCenter(this); + notificationCenter = new NotificationCenter(this); startThreads(0); } @@ -446,6 +446,7 @@ public class ApplicationDcContext extends DcContext { } public void stopThreads() { + notificationCenter.removeAllNotifiations(); run = false; synchronized (threadsCritical) { while (true) { @@ -580,7 +581,7 @@ public class ApplicationDcContext extends DcContext { break; case DC_EVENT_INCOMING_MSG: - Util.runOnAnyBackgroundThread(() -> notificationManger.addNotification((int) data1, (int) data2)); + notificationCenter.addNotification((int) data1, (int) data2); if (eventCenter != null) { eventCenter.sendToObservers(event, data1, data2); // Other parts of the code are also interested in this event } diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java b/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java index 0cb74d6d8..c5f874962 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java @@ -257,6 +257,13 @@ public class NotificationCenter { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + // get notification text + DcMsg dcMsg = dcContext.getMsg(msgId); + String text = dcMsg.getSummarytext(100); + if (dcChat.isGroup()) { + text = dcContext.getContact(dcMsg.getFromId()).getFirstName() + ": " + text; + } + // create a basic notification NotificationCompat.Builder builder = new NotificationCompat.Builder(context, getNotificationChannel(notificationManager, dcChat)) .setSmallIcon(R.drawable.icon_notification) @@ -264,7 +271,7 @@ public class NotificationCenter { .setPriority(Prefs.getNotificationPriority(context)) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setContentTitle(dcChat.getName()) - .setContentText(dcContext.getMsg(msgId).getSummarytext(100)); + .setContentText(text); // set sound, vibrate, led for systems that do not have notification channels if (!notificationChannelsSupported()) { @@ -282,22 +289,29 @@ public class NotificationCenter { } } - // add notification - notificationManager.notify(ID_MSG_OFFSET+msgId, builder.build()); + // add notification, we use one notification per chat, + // esp. older android are not that great at grouping + notificationManager.notify(ID_MSG_OFFSET+chatId, builder.build()); }); } public void removeNotifications(int chatId) { - Util.runOnAnyBackgroundThread(() -> { + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + notificationManager.cancel(ID_MSG_OFFSET + chatId); + } - }); + public void removeAllNotifiations() { + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + notificationManager.cancelAll(); } public void updateVisibleChat(int chatId) { Util.runOnAnyBackgroundThread(() -> { visibleChatId = chatId; - removeNotifications(chatId); + if (chatId!=0) { + removeNotifications(chatId); + } }); }