From 4c2902a39e19916f05d6e06a3dc84722efa44826 Mon Sep 17 00:00:00 2001 From: schwma <37244550+schwma@users.noreply.github.com> Date: Sun, 11 Nov 2018 02:37:39 +0100 Subject: [PATCH 1/4] Map Gotify message priorities to notification channels --- .../github/gotify/NotificationSupport.java | 71 ++++++++++++++++--- .../gotify/service/WebSocketService.java | 17 +++-- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/gotify/NotificationSupport.java b/app/src/main/java/com/github/gotify/NotificationSupport.java index 9037cad7..2db9e618 100644 --- a/app/src/main/java/com/github/gotify/NotificationSupport.java +++ b/app/src/main/java/com/github/gotify/NotificationSupport.java @@ -14,7 +14,10 @@ public static final class Group { public static final class Channel { public static final String FOREGROUND = "gotify_foreground"; - public static final String MESSAGES = "gotify_messages"; + public static final String MESSAGES_IMPORTANCE_MIN = "gotify_messages_min_importance"; + public static final String MESSAGES_IMPORTANCE_LOW = "gotify_messages_low_importance"; + public static final String MESSAGES_IMPORTANCE_DEFAULT = "gotify_messages_default_importance"; + public static final String MESSAGES_IMPORTANCE_HIGH = "gotify_messages_high_importance"; } public static final class ID { @@ -32,19 +35,69 @@ public static void createChannels(NotificationManager notificationManager) { Channel.FOREGROUND, "Gotify foreground notification", NotificationManager.IMPORTANCE_LOW); - // High importance for message notifications so that they are shown as heads-up - // notifications and sorted towards the top of the notification shade - NotificationChannel messages = + + NotificationChannel messagesImportanceMin = + new NotificationChannel( + Channel.MESSAGES_IMPORTANCE_MIN, + "Min importance Gotify messages", + NotificationManager.IMPORTANCE_MIN); + + NotificationChannel messagesImportanceLow = + new NotificationChannel( + Channel.MESSAGES_IMPORTANCE_LOW, + "Low importance Gotify messages", + NotificationManager.IMPORTANCE_LOW); + + NotificationChannel messagesImportanceDefault = new NotificationChannel( - Channel.MESSAGES, - "Gotify messages", + Channel.MESSAGES_IMPORTANCE_DEFAULT, + "Default importance Gotify messages", + NotificationManager.IMPORTANCE_DEFAULT); + messagesImportanceDefault.enableLights(true); + messagesImportanceDefault.setLightColor(Color.CYAN); + + NotificationChannel messagesImportanceHigh = + new NotificationChannel( + Channel.MESSAGES_IMPORTANCE_HIGH, + "High importance Gotify messages", NotificationManager.IMPORTANCE_HIGH); - messages.enableLights(true); - messages.setLightColor(Color.CYAN); + messagesImportanceHigh.enableLights(true); + messagesImportanceHigh.setLightColor(Color.CYAN); + notificationManager.createNotificationChannel(foreground); - notificationManager.createNotificationChannel(messages); + notificationManager.createNotificationChannel(messagesImportanceMin); + notificationManager.createNotificationChannel(messagesImportanceLow); + notificationManager.createNotificationChannel(messagesImportanceDefault); + notificationManager.createNotificationChannel(messagesImportanceHigh); } catch (Exception e) { Log.e("Could not create channel", e); } } + + /** + * Map {@link com.github.gotify.client.model.Message#getPriority() Gotify message priorities} + * to Android channels. + * + *
+     * Gotify Priority  | Android Importance
+     * <= 0             | min
+     * 1-3              | low
+     * 4-7              | default
+     * >= 8             | high
+     * 
+ * + * @param priority the Gotify priority to convert to a notification channel as a long. + * @return the identifier of the notification channel as a String. + */ + public static String convertPriorityToChannel(long priority) { + if (priority < 1) { + return Channel.MESSAGES_IMPORTANCE_MIN; + } else if (priority < 4) { + return Channel.MESSAGES_IMPORTANCE_LOW; + } else if (priority < 8) { + return Channel.MESSAGES_IMPORTANCE_DEFAULT; + } else { + return Channel.MESSAGES_IMPORTANCE_HIGH; + } + } } diff --git a/app/src/main/java/com/github/gotify/service/WebSocketService.java b/app/src/main/java/com/github/gotify/service/WebSocketService.java index 92426f33..7b0a1eb7 100644 --- a/app/src/main/java/com/github/gotify/service/WebSocketService.java +++ b/app/src/main/java/com/github/gotify/service/WebSocketService.java @@ -117,9 +117,11 @@ private void notifyMissedNotifications() { } private void onGroupedMessages(List messages) { + long highestPriority = 0; for (Message message : messages) { if (lastReceivedMessage.get() < message.getId()) { lastReceivedMessage.set(message.getId()); + highestPriority = Math.max(highestPriority, message.getPriority()); } broadcast(message); } @@ -127,7 +129,8 @@ private void onGroupedMessages(List messages) { showNotification( NotificationSupport.ID.GROUPED, getString(R.string.missed_messages), - getString(R.string.grouped_message, size)); + getString(R.string.grouped_message, size), + highestPriority); } private void onMessage(Message message) { @@ -135,7 +138,7 @@ private void onMessage(Message message) { lastReceivedMessage.set(message.getId()); } broadcast(message); - showNotification(message.getId(), message.getTitle(), message.getMessage()); + showNotification(message.getId(), message.getTitle(), message.getMessage(), message.getPriority()); } private void broadcast(Message message) { @@ -172,16 +175,16 @@ private void foreground(String message) { startForeground(NotificationSupport.ID.FOREGROUND, notification); } - private void showNotification(int id, String title, String message) { + private void showNotification(int id, String title, String message, long priority) { Intent intent = new Intent(this, MessagesActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = - new NotificationCompat.Builder(this, NotificationSupport.Channel.MESSAGES); + new NotificationCompat.Builder(this, NotificationSupport.convertPriorityToChannel(priority)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - showNotificationGroup(); + showNotificationGroup(priority); } b.setAutoCancel(true) @@ -204,13 +207,13 @@ private void showNotification(int id, String title, String message) { } @RequiresApi(Build.VERSION_CODES.N) - public void showNotificationGroup() { + public void showNotificationGroup(long priority) { Intent intent = new Intent(this, MessagesActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = - new NotificationCompat.Builder(this, NotificationSupport.Channel.MESSAGES); + new NotificationCompat.Builder(this, NotificationSupport.convertPriorityToChannel(priority)); b.setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) From 52d9329f5020c2d41e52e90cd3f847f0851e5fb4 Mon Sep 17 00:00:00 2001 From: schwma <37244550+schwma@users.noreply.github.com> Date: Sun, 11 Nov 2018 03:16:21 +0100 Subject: [PATCH 2/4] Fix formatting as suggested by spotlessJava --- .../main/java/com/github/gotify/NotificationSupport.java | 7 ++++--- .../java/com/github/gotify/service/WebSocketService.java | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/github/gotify/NotificationSupport.java b/app/src/main/java/com/github/gotify/NotificationSupport.java index 2db9e618..c280bae2 100644 --- a/app/src/main/java/com/github/gotify/NotificationSupport.java +++ b/app/src/main/java/com/github/gotify/NotificationSupport.java @@ -16,7 +16,8 @@ public static final class Channel { public static final String FOREGROUND = "gotify_foreground"; public static final String MESSAGES_IMPORTANCE_MIN = "gotify_messages_min_importance"; public static final String MESSAGES_IMPORTANCE_LOW = "gotify_messages_low_importance"; - public static final String MESSAGES_IMPORTANCE_DEFAULT = "gotify_messages_default_importance"; + public static final String MESSAGES_IMPORTANCE_DEFAULT = + "gotify_messages_default_importance"; public static final String MESSAGES_IMPORTANCE_HIGH = "gotify_messages_high_importance"; } @@ -75,8 +76,8 @@ public static void createChannels(NotificationManager notificationManager) { } /** - * Map {@link com.github.gotify.client.model.Message#getPriority() Gotify message priorities} - * to Android channels. + * Map {@link com.github.gotify.client.model.Message#getPriority() Gotify message priorities to + * Android channels. * *
      * Gotify Priority  | Android Importance
diff --git a/app/src/main/java/com/github/gotify/service/WebSocketService.java b/app/src/main/java/com/github/gotify/service/WebSocketService.java
index 7b0a1eb7..a08d805d 100644
--- a/app/src/main/java/com/github/gotify/service/WebSocketService.java
+++ b/app/src/main/java/com/github/gotify/service/WebSocketService.java
@@ -138,7 +138,8 @@ private void onMessage(Message message) {
             lastReceivedMessage.set(message.getId());
         }
         broadcast(message);
-        showNotification(message.getId(), message.getTitle(), message.getMessage(), message.getPriority());
+        showNotification(
+                message.getId(), message.getTitle(), message.getMessage(), message.getPriority());
     }
 
     private void broadcast(Message message) {
@@ -181,7 +182,8 @@ private void showNotification(int id, String title, String message, long priorit
                 PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 
         NotificationCompat.Builder b =
-                new NotificationCompat.Builder(this, NotificationSupport.convertPriorityToChannel(priority));
+                new NotificationCompat.Builder(
+                        this, NotificationSupport.convertPriorityToChannel(priority));
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             showNotificationGroup(priority);
@@ -213,7 +215,8 @@ public void showNotificationGroup(long priority) {
                 PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 
         NotificationCompat.Builder b =
-                new NotificationCompat.Builder(this, NotificationSupport.convertPriorityToChannel(priority));
+                new NotificationCompat.Builder(
+                        this, NotificationSupport.convertPriorityToChannel(priority));
 
         b.setAutoCancel(true)
                 .setDefaults(Notification.DEFAULT_ALL)

From 63af9d458dd9855daf308865cd6ffc75cfb202a6 Mon Sep 17 00:00:00 2001
From: schwma <37244550+schwma@users.noreply.github.com>
Date: Sun, 11 Nov 2018 15:22:22 +0100
Subject: [PATCH 3/4] Adjust notification channel names to include their
 corresponding gotify priorities

---
 .../main/java/com/github/gotify/NotificationSupport.java  | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/app/src/main/java/com/github/gotify/NotificationSupport.java b/app/src/main/java/com/github/gotify/NotificationSupport.java
index c280bae2..6d0e2a1e 100644
--- a/app/src/main/java/com/github/gotify/NotificationSupport.java
+++ b/app/src/main/java/com/github/gotify/NotificationSupport.java
@@ -40,19 +40,19 @@ public static void createChannels(NotificationManager notificationManager) {
             NotificationChannel messagesImportanceMin =
                     new NotificationChannel(
                             Channel.MESSAGES_IMPORTANCE_MIN,
-                            "Min importance Gotify messages",
+                            "Min priority messages (<1)",
                             NotificationManager.IMPORTANCE_MIN);
 
             NotificationChannel messagesImportanceLow =
                     new NotificationChannel(
                             Channel.MESSAGES_IMPORTANCE_LOW,
-                            "Low importance Gotify messages",
+                            "Low priority messages (1-3)",
                             NotificationManager.IMPORTANCE_LOW);
 
             NotificationChannel messagesImportanceDefault =
                     new NotificationChannel(
                             Channel.MESSAGES_IMPORTANCE_DEFAULT,
-                            "Default importance Gotify messages",
+                            "Normal priority messages (4-7)",
                             NotificationManager.IMPORTANCE_DEFAULT);
             messagesImportanceDefault.enableLights(true);
             messagesImportanceDefault.setLightColor(Color.CYAN);
@@ -60,7 +60,7 @@ public static void createChannels(NotificationManager notificationManager) {
             NotificationChannel messagesImportanceHigh =
                     new NotificationChannel(
                             Channel.MESSAGES_IMPORTANCE_HIGH,
-                            "High importance Gotify messages",
+                            "High priority messages (>7)",
                             NotificationManager.IMPORTANCE_HIGH);
             messagesImportanceHigh.enableLights(true);
             messagesImportanceHigh.setLightColor(Color.CYAN);

From abd40ea56c70cc1960200f0ec14b9f7839f1aa4b Mon Sep 17 00:00:00 2001
From: schwma <37244550+schwma@users.noreply.github.com>
Date: Sun, 11 Nov 2018 16:52:21 +0100
Subject: [PATCH 4/4] Enable vibration by default for high and default
 importance notification channels

---
 app/src/main/java/com/github/gotify/NotificationSupport.java | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/app/src/main/java/com/github/gotify/NotificationSupport.java b/app/src/main/java/com/github/gotify/NotificationSupport.java
index 6d0e2a1e..fdd34df2 100644
--- a/app/src/main/java/com/github/gotify/NotificationSupport.java
+++ b/app/src/main/java/com/github/gotify/NotificationSupport.java
@@ -56,6 +56,7 @@ public static void createChannels(NotificationManager notificationManager) {
                             NotificationManager.IMPORTANCE_DEFAULT);
             messagesImportanceDefault.enableLights(true);
             messagesImportanceDefault.setLightColor(Color.CYAN);
+            messagesImportanceDefault.enableVibration(true);
 
             NotificationChannel messagesImportanceHigh =
                     new NotificationChannel(
@@ -64,6 +65,7 @@ public static void createChannels(NotificationManager notificationManager) {
                             NotificationManager.IMPORTANCE_HIGH);
             messagesImportanceHigh.enableLights(true);
             messagesImportanceHigh.setLightColor(Color.CYAN);
+            messagesImportanceHigh.enableVibration(true);
 
             notificationManager.createNotificationChannel(foreground);
             notificationManager.createNotificationChannel(messagesImportanceMin);