Эх сурвалжийг харах

Some magic notification channels

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 жил өмнө
parent
commit
035af9a385

+ 41 - 4
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) {
+
+                                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()));
+                            }
 
-                            NotificationManager notificationManager =
-                                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+                            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());

+ 66 - 0
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 <mario@lovelyhq.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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);
+            }
+        }
+    }
+}

+ 0 - 2
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;
     }
-
 }

+ 7 - 0
app/src/main/res/values/strings.xml

@@ -100,4 +100,11 @@
     <!-- Call -->
     <string name="nc_incoming_call">Incoming call</string>
 
+    <!-- Notification channels -->
+    <string name="nc_notification_channel">%1$s on %2$s notification channel</string>
+    <string name="nc_notification_channel_calls">Calls notification channel</string>
+    <string name="nc_notification_channel_messages">Messages notification channel</string>
+    <string name="nc_notification_channel_calls_description">Shows incoming calls</string>
+    <string name="nc_notification_channel_messages_description">Shows incoming messages</string>
+
 </resources>