Updates to notifications handling

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2019-06-25 22:41:02 +02:00
parent 2a90560056
commit 323d9e16b1
5 changed files with 110 additions and 61 deletions

View File

@ -87,8 +87,6 @@ import io.reactivex.Observable;
import io.reactivex.Observer; import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BooleanSupplier;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import me.zhanghai.android.effortlesspermissions.AfterPermissionDenied; import me.zhanghai.android.effortlesspermissions.AfterPermissionDenied;
import me.zhanghai.android.effortlesspermissions.EffortlessPermissions; import me.zhanghai.android.effortlesspermissions.EffortlessPermissions;
@ -1123,9 +1121,9 @@ public class CallController extends BaseController {
} }
if (!conversationUser.hasSpreedCapabilityWithName("no-ping") && !TextUtils.isEmpty(roomId)) { if (!conversationUser.hasSpreedCapabilityWithName("no-ping") && !TextUtils.isEmpty(roomId)) {
NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomId); NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomId);
} else if (!TextUtils.isEmpty(roomToken)) { } else if (!TextUtils.isEmpty(roomToken)) {
NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomToken); NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomToken);
} }
if (!hasExternalSignalingServer) { if (!hasExternalSignalingServer) {

View File

@ -566,9 +566,9 @@ public class ChatController extends BaseController implements MessagesListAdapte
private void cancelNotificationsForCurrentConversation() { private void cancelNotificationsForCurrentConversation() {
if (!conversationUser.hasSpreedCapabilityWithName("no-ping") && !TextUtils.isEmpty(roomId)) { if (!conversationUser.hasSpreedCapabilityWithName("no-ping") && !TextUtils.isEmpty(roomId)) {
NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomId); NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomId);
} else if (!TextUtils.isEmpty(roomToken)){ } else if (!TextUtils.isEmpty(roomToken)){
NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomToken); NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomToken);
} }
} }

View File

@ -258,14 +258,12 @@ public class NotificationWorker extends Worker {
} }
} }
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, PendingIntent pendingIntent = PendingIntent.getActivity(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
CRC32 crc32 = new CRC32();
Uri uri = Uri.parse(signatureVerification.getUserEntity().getBaseUrl()); Uri uri = Uri.parse(signatureVerification.getUserEntity().getBaseUrl());
String baseUrl = uri.getHost(); String baseUrl = uri.getHost();
@ -291,9 +289,12 @@ public class NotificationWorker extends Worker {
notificationBuilder.setColor(context.getResources().getColor(R.color.colorPrimary)); notificationBuilder.setColor(context.getResources().getColor(R.color.colorPrimary));
} }
String groupName = signatureVerification.getUserEntity().getId() + "@" + decryptedPushMessage.getId(); Bundle notificationInfo = new Bundle();
crc32.update(groupName.getBytes()); notificationInfo.putLong(BundleKeys.KEY_INTERNAL_USER_ID, signatureVerification.getUserEntity().getId());
notificationBuilder.setGroup(Long.toString(crc32.getValue())); // could be an ID or a TOKEN
notificationInfo.putString(BundleKeys.KEY_ROOM_TOKEN, decryptedPushMessage.getId());
notificationInfo.putLong(BundleKeys.KEY_NOTIFICATION_ID, decryptedPushMessage.getNotificationId());
notificationBuilder.setExtras(notificationInfo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -326,9 +327,13 @@ public class NotificationWorker extends Worker {
notificationBuilder.setContentIntent(pendingIntent); notificationBuilder.setContentIntent(pendingIntent);
String stringForCrc = decryptedPushMessage.getSubject() + " " + signatureVerification String stringForCrc = String.valueOf(System.currentTimeMillis());
.getUserEntity().getDisplayName() + " " + signatureVerification.getUserEntity
().getBaseUrl() + System.currentTimeMillis(); CRC32 crc32 = new CRC32();
String groupName = signatureVerification.getUserEntity().getId() + "@" + decryptedPushMessage.getId();
crc32.update(groupName.getBytes());
notificationBuilder.setGroup(Long.toString(crc32.getValue()));
crc32 = new CRC32(); crc32 = new CRC32();
crc32.update(stringForCrc.getBytes()); crc32.update(stringForCrc.getBytes());
@ -350,7 +355,6 @@ public class NotificationWorker extends Worker {
} }
if (notificationManager != null) {
notificationManager.notify((int) crc32.getValue(), notificationBuilder.build()); notificationManager.notify((int) crc32.getValue(), notificationBuilder.build());
if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() && if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() &&
@ -392,7 +396,6 @@ public class NotificationWorker extends Worker {
} }
} }
} }
}
@NonNull @NonNull
@Override @Override
@ -444,8 +447,7 @@ public class NotificationWorker extends Worker {
intent = new Intent(context, MainActivity.class); intent = new Intent(context, MainActivity.class);
} }
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (!signatureVerification.getUserEntity().hasSpreedCapabilityWithName if (!signatureVerification.getUserEntity().hasSpreedCapabilityWithName
("no-ping")) { ("no-ping")) {

View File

@ -21,6 +21,7 @@
package com.nextcloud.talk.utils; package com.nextcloud.talk.utils;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationChannelGroup; import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -28,8 +29,10 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.text.TextUtils; import android.text.TextUtils;
import com.nextcloud.talk.R; import com.nextcloud.talk.R;
import com.nextcloud.talk.models.database.UserEntity; import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import java.util.zip.CRC32; import java.util.zip.CRC32;
@ -79,7 +82,55 @@ public class NotificationUtils {
} }
} }
public static void cancelExistingNotifications(Context context, UserEntity conversationUser, public static void cancelAllNotificationsForAccount(Context context, UserEntity conversationUser) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 &&
context != null) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications();
Notification notification;
for (StatusBarNotification statusBarNotification : statusBarNotifications) {
notification = statusBarNotification.getNotification();
if (notification != null && !notification.extras.isEmpty()) {
if (conversationUser.getId() == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)) {
notificationManager.cancel(statusBarNotification.getId());
}
}
}
}
}
}
public static void cancelExistingNotificationWithId(Context context, UserEntity conversationUser, long notificationId) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 &&
context != null) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications();
Notification notification;
for (StatusBarNotification statusBarNotification : statusBarNotifications) {
notification = statusBarNotification.getNotification();
if (notification != null && !notification.extras.isEmpty()) {
if (conversationUser.getId() == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)) {
notificationManager.cancel(statusBarNotification.getId());
}
}
}
}
}
}
public static void cancelExistingNotificationsForRoom(Context context, UserEntity conversationUser,
String roomTokenOrId) { String roomTokenOrId) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 && if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 &&
context != null) { context != null) {
@ -87,18 +138,15 @@ public class NotificationUtils {
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CRC32 crc32 = new CRC32();
String groupName = conversationUser.getId() + "@" + roomTokenOrId;
crc32.update(groupName.getBytes());
String crc32GroupString = Long.toString(crc32.getValue());
if (notificationManager != null) { if (notificationManager != null) {
StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications(); StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications();
Notification notification;
for (StatusBarNotification statusBarNotification : statusBarNotifications) { for (StatusBarNotification statusBarNotification : statusBarNotifications) {
notification = statusBarNotification.getNotification();
if (statusBarNotification.getNotification() != null && if (notification != null && !notification.extras.isEmpty()) {
!TextUtils.isEmpty(statusBarNotification.getNotification().getGroup())) { if (conversationUser.getId() == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
if (statusBarNotification.getNotification().getGroup().equals(crc32GroupString)) { roomTokenOrId.equals(statusBarNotification.getNotification().extras.getString(BundleKeys.KEY_ROOM_TOKEN))) {
notificationManager.cancel(statusBarNotification.getId()); notificationManager.cancel(statusBarNotification.getId());
} }
} }

View File

@ -55,4 +55,5 @@ public class BundleKeys {
public static final String KEY_FILE_PATHS = "KEY_FILE_PATHS"; public static final String KEY_FILE_PATHS = "KEY_FILE_PATHS";
public static final String KEY_ACCOUNT = "KEY_ACCOUNT"; public static final String KEY_ACCOUNT = "KEY_ACCOUNT";
public static final String KEY_FILE_ID = "KEY_FILE_ID"; public static final String KEY_FILE_ID = "KEY_FILE_ID";
public static final String KEY_NOTIFICATION_ID = "KEY_NOTIFICATION_ID";
} }