diff --git a/app/src/main/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java b/app/src/main/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java index f70fc2301..99c35bb21 100644 --- a/app/src/main/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java +++ b/app/src/main/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java @@ -42,6 +42,7 @@ import com.nextcloud.talk.activities.CallActivity; import com.nextcloud.talk.api.models.json.push.DecryptedPushMessage; import com.nextcloud.talk.api.models.json.push.PushMessage; import com.nextcloud.talk.models.SignatureVerification; +import com.nextcloud.talk.utils.NotificationUtils; import com.nextcloud.talk.utils.PushUtils; import com.nextcloud.talk.utils.bundle.BundleBuilder; @@ -98,6 +99,9 @@ public class MagicFirebaseMessagingService extends FirebaseMessagingService { PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + switch (decryptedPushMessage.getType()) { case "call": smallIcon = R.drawable.ic_call_black_24dp; @@ -119,6 +123,8 @@ public class MagicFirebaseMessagingService extends FirebaseMessagingService { } largeIcon = BitmapFactory.decodeResource(getResources(), smallIcon); + CRC32 crc32 = new CRC32(); + Notification.Builder notificationBuilder = new Notification.Builder(this) .setSmallIcon(smallIcon) @@ -133,17 +139,48 @@ public class MagicFirebaseMessagingService extends FirebaseMessagingService { .setSound(soundUri) .setAutoCancel(true); - notificationBuilder.setContentIntent(pendingIntent); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - NotificationManager notificationManager = - (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + String usernameAndServerCrc32 = signatureVerification.getUserEntity().getUsername() + + "@" + signatureVerification.getUserEntity().getBaseUrl(); + crc32.update(usernameAndServerCrc32.getBytes()); + String groupName = String.format(getResources().getString(R.string + .nc_notification_channel), signatureVerification.getUserEntity() + .getDisplayName(), signatureVerification.getUserEntity().getBaseUrl()); + + NotificationUtils.createNotificationChannelGroup(notificationManager, + Long.toString(crc32.getValue()), + groupName); + + if (category.equals(Notification.CATEGORY_CALL)) { + NotificationUtils.createNotificationChannel(notificationManager, + NotificationUtils.NOTIFICATION_CHANNEL_CALLS, getResources().getString(R + .string.nc_notification_channel_calls), getResources().getString + (R.string.nc_notification_channel_calls_description), true, + NotificationManager.IMPORTANCE_HIGH); + + notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_CALLS); + } else { + NotificationUtils.createNotificationChannel(notificationManager, + NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES, getResources().getString(R + .string.nc_notification_channel_messages), getResources().getString + (R.string.nc_notification_channel_messages_description), true, + NotificationManager.IMPORTANCE_DEFAULT); + + notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES); + } + + notificationBuilder.setGroup(Long.toString(crc32.getValue())); + } + + notificationBuilder.setContentIntent(pendingIntent); if (notificationManager != null) { String stringForCrc = decryptedPushMessage.getSubject() + " " + signatureVerification .getUserEntity().getDisplayName() + " " + signatureVerification.getUserEntity ().getBaseUrl(); - CRC32 crc32 = new CRC32(); + crc32 = new CRC32(); crc32.update(stringForCrc.getBytes()); notificationManager.notify((int) crc32.getValue(), notificationBuilder.build()); diff --git a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java new file mode 100644 index 000000000..98c28ad2a --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java @@ -0,0 +1,66 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.utils; + +import android.annotation.TargetApi; +import android.app.NotificationChannel; +import android.app.NotificationChannelGroup; +import android.app.NotificationManager; +import android.graphics.Color; +import android.os.Build; + +public class NotificationUtils { + public static final String NOTIFICATION_CHANNEL_CALLS = "NOTIFICATION_CHANNEL_CALLS"; + public static final String NOTIFICATION_CHANNEL_MESSAGES = "NOTIFICATION_CHANNEL_MESSAGES"; + private static final String TAG = "NotificationUtils"; + + @TargetApi(Build.VERSION_CODES.O) + public static void createNotificationChannel(NotificationManager notificationManager, + String channelId, String channelName, + String channelDescription, boolean vibrate, + int importance) { + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O + && notificationManager.getNotificationChannel(channelId) == null) { + + NotificationChannel channel = new NotificationChannel(channelId, channelName, + importance); + + channel.setDescription(channelDescription); + channel.enableLights(vibrate); + channel.enableVibration(vibrate); + + channel.setLightColor(Color.RED); + notificationManager.createNotificationChannel(channel); + } + } + + @TargetApi(Build.VERSION_CODES.O) + public static void createNotificationChannelGroup(NotificationManager notificationManager, + String groupId, CharSequence groupName) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup(groupId, groupName); + if (!notificationManager.getNotificationChannelGroups().contains(notificationChannelGroup)) { + notificationManager.createNotificationChannelGroup(notificationChannelGroup); + } + } + } +} diff --git a/app/src/main/java/com/nextcloud/talk/utils/PushUtils.java b/app/src/main/java/com/nextcloud/talk/utils/PushUtils.java index 9f979c0cb..ed528dad2 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/PushUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/PushUtils.java @@ -105,7 +105,6 @@ public class PushUtils { getString(R.string.nc_push_server_url); } - public SignatureVerification verifySignature(byte[] signatureBytes, byte[] subjectBytes) { Signature signature = null; PushConfigurationState pushConfigurationState; @@ -412,5 +411,4 @@ public class PushUtils { return null; } - } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c8255723a..b8eef6c5f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -100,4 +100,11 @@ Incoming call + + %1$s on %2$s notification channel + Calls notification channel + Messages notification channel + Shows incoming calls + Shows incoming messages +