Merge pull request #22 from schwma/master
Map Gotify message priorities to notification channels
This commit is contained in:
@@ -14,7 +14,11 @@ public class NotificationSupport {
|
|||||||
|
|
||||||
public static final class Channel {
|
public static final class Channel {
|
||||||
public static final String FOREGROUND = "gotify_foreground";
|
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 {
|
public static final class ID {
|
||||||
@@ -32,19 +36,71 @@ public class NotificationSupport {
|
|||||||
Channel.FOREGROUND,
|
Channel.FOREGROUND,
|
||||||
"Gotify foreground notification",
|
"Gotify foreground notification",
|
||||||
NotificationManager.IMPORTANCE_LOW);
|
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 messagesImportanceMin =
|
||||||
NotificationChannel messages =
|
|
||||||
new NotificationChannel(
|
new NotificationChannel(
|
||||||
Channel.MESSAGES,
|
Channel.MESSAGES_IMPORTANCE_MIN,
|
||||||
"Gotify messages",
|
"Min priority messages (<1)",
|
||||||
|
NotificationManager.IMPORTANCE_MIN);
|
||||||
|
|
||||||
|
NotificationChannel messagesImportanceLow =
|
||||||
|
new NotificationChannel(
|
||||||
|
Channel.MESSAGES_IMPORTANCE_LOW,
|
||||||
|
"Low priority messages (1-3)",
|
||||||
|
NotificationManager.IMPORTANCE_LOW);
|
||||||
|
|
||||||
|
NotificationChannel messagesImportanceDefault =
|
||||||
|
new NotificationChannel(
|
||||||
|
Channel.MESSAGES_IMPORTANCE_DEFAULT,
|
||||||
|
"Normal priority messages (4-7)",
|
||||||
|
NotificationManager.IMPORTANCE_DEFAULT);
|
||||||
|
messagesImportanceDefault.enableLights(true);
|
||||||
|
messagesImportanceDefault.setLightColor(Color.CYAN);
|
||||||
|
messagesImportanceDefault.enableVibration(true);
|
||||||
|
|
||||||
|
NotificationChannel messagesImportanceHigh =
|
||||||
|
new NotificationChannel(
|
||||||
|
Channel.MESSAGES_IMPORTANCE_HIGH,
|
||||||
|
"High priority messages (>7)",
|
||||||
NotificationManager.IMPORTANCE_HIGH);
|
NotificationManager.IMPORTANCE_HIGH);
|
||||||
messages.enableLights(true);
|
messagesImportanceHigh.enableLights(true);
|
||||||
messages.setLightColor(Color.CYAN);
|
messagesImportanceHigh.setLightColor(Color.CYAN);
|
||||||
|
messagesImportanceHigh.enableVibration(true);
|
||||||
|
|
||||||
notificationManager.createNotificationChannel(foreground);
|
notificationManager.createNotificationChannel(foreground);
|
||||||
notificationManager.createNotificationChannel(messages);
|
notificationManager.createNotificationChannel(messagesImportanceMin);
|
||||||
|
notificationManager.createNotificationChannel(messagesImportanceLow);
|
||||||
|
notificationManager.createNotificationChannel(messagesImportanceDefault);
|
||||||
|
notificationManager.createNotificationChannel(messagesImportanceHigh);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("Could not create channel", 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,9 +119,11 @@ public class WebSocketService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onGroupedMessages(List<Message> messages) {
|
private void onGroupedMessages(List<Message> messages) {
|
||||||
|
long highestPriority = 0;
|
||||||
for (Message message : messages) {
|
for (Message message : messages) {
|
||||||
if (lastReceivedMessage.get() < message.getId()) {
|
if (lastReceivedMessage.get() < message.getId()) {
|
||||||
lastReceivedMessage.set(message.getId());
|
lastReceivedMessage.set(message.getId());
|
||||||
|
highestPriority = Math.max(highestPriority, message.getPriority());
|
||||||
}
|
}
|
||||||
broadcast(message);
|
broadcast(message);
|
||||||
}
|
}
|
||||||
@@ -129,7 +131,8 @@ public class WebSocketService extends Service {
|
|||||||
showNotification(
|
showNotification(
|
||||||
NotificationSupport.ID.GROUPED,
|
NotificationSupport.ID.GROUPED,
|
||||||
getString(R.string.missed_messages),
|
getString(R.string.missed_messages),
|
||||||
getString(R.string.grouped_message, size));
|
getString(R.string.grouped_message, size),
|
||||||
|
highestPriority);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onMessage(Message message) {
|
private void onMessage(Message message) {
|
||||||
@@ -137,7 +140,8 @@ public class WebSocketService extends Service {
|
|||||||
lastReceivedMessage.set(message.getId());
|
lastReceivedMessage.set(message.getId());
|
||||||
}
|
}
|
||||||
broadcast(message);
|
broadcast(message);
|
||||||
showNotification(message.getId(), message.getTitle(), message.getMessage());
|
showNotification(
|
||||||
|
message.getId(), message.getTitle(), message.getMessage(), message.getPriority());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void broadcast(Message message) {
|
private void broadcast(Message message) {
|
||||||
@@ -174,16 +178,17 @@ public class WebSocketService extends Service {
|
|||||||
startForeground(NotificationSupport.ID.FOREGROUND, notification);
|
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);
|
Intent intent = new Intent(this, MessagesActivity.class);
|
||||||
PendingIntent contentIntent =
|
PendingIntent contentIntent =
|
||||||
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
NotificationCompat.Builder b =
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
showNotificationGroup();
|
showNotificationGroup(priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
b.setAutoCancel(true)
|
b.setAutoCancel(true)
|
||||||
@@ -206,13 +211,14 @@ public class WebSocketService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.N)
|
@RequiresApi(Build.VERSION_CODES.N)
|
||||||
public void showNotificationGroup() {
|
public void showNotificationGroup(long priority) {
|
||||||
Intent intent = new Intent(this, MessagesActivity.class);
|
Intent intent = new Intent(this, MessagesActivity.class);
|
||||||
PendingIntent contentIntent =
|
PendingIntent contentIntent =
|
||||||
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
NotificationCompat.Builder b =
|
NotificationCompat.Builder b =
|
||||||
new NotificationCompat.Builder(this, NotificationSupport.Channel.MESSAGES);
|
new NotificationCompat.Builder(
|
||||||
|
this, NotificationSupport.convertPriorityToChannel(priority));
|
||||||
|
|
||||||
b.setAutoCancel(true)
|
b.setAutoCancel(true)
|
||||||
.setDefaults(Notification.DEFAULT_ALL)
|
.setDefaults(Notification.DEFAULT_ALL)
|
||||||
|
|||||||
Reference in New Issue
Block a user