Map Gotify message priorities to notification channels

This commit is contained in:
schwma
2018-11-11 02:37:39 +01:00
parent b306c319e7
commit 4c2902a39e
2 changed files with 72 additions and 16 deletions

View File

@@ -14,7 +14,10 @@ public class NotificationSupport {
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 class NotificationSupport {
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,
"Gotify messages",
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_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.
*
* <pre>
* Gotify Priority | Android Importance
* <= 0 | min
* 1-3 | low
* 4-7 | default
* >= 8 | high
* </pre>
*
* @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;
}
}
}