From 1ee51cdb12467993bbe28c34dfa3fa16c410877a Mon Sep 17 00:00:00 2001 From: Niels Date: Fri, 10 Jul 2020 21:56:35 +0200 Subject: [PATCH] feat(android): add option to make a notification ongoing (#3165) --- .../notification/LocalNotification.java | 28 ++++++++++++++++++- .../LocalNotificationManager.java | 4 +-- core/src/core-plugin-definitions.ts | 9 ++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java index b00847c20..3fbabb64e 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java @@ -33,6 +33,8 @@ public class LocalNotification { private String actionTypeId; private String group; private boolean groupSummary; + private boolean ongoing; + private boolean autoCancel; private JSObject extra; private List attachments; private LocalNotificationSchedule schedule; @@ -152,6 +154,22 @@ public void setGroupSummary(boolean groupSummary) { this.groupSummary = groupSummary; } + public boolean isOngoing() { + return ongoing; + } + + public void setOngoing(boolean ongoing) { + this.ongoing = ongoing; + } + + public boolean isAutoCancel() { + return autoCancel; + } + + public void setAutoCancel(boolean autoCancel) { + this.autoCancel = autoCancel; + } + public String getChannelId() { return channelId; } @@ -186,7 +204,7 @@ public static List buildNotificationList(PluginCall call) { call.error("Invalid JSON object sent to NotificationPlugin", e); return null; } - + try { LocalNotification activeLocalNotification = buildNotificationFromJSObject(notification); resultLocalNotifications.add(activeLocalNotification); @@ -214,6 +232,8 @@ public static LocalNotification buildNotificationFromJSObject(JSObject jsonObjec localNotification.setChannelId(jsonObject.getString("channelId")); localNotification.setSchedule(new LocalNotificationSchedule(jsonObject)); localNotification.setExtra(jsonObject.getJSObject("extra")); + localNotification.setOngoing(jsonObject.getBoolean("ongoing", false)); + localNotification.setAutoCancel(jsonObject.getBoolean("autoCancel", true)); return localNotification; } @@ -288,6 +308,8 @@ public String toString() { ", attachments=" + attachments + ", schedule=" + schedule + ", groupSummary=" + groupSummary + + ", ongoing=" + ongoing + + ", autoCancel=" + autoCancel + '}'; } @@ -311,6 +333,8 @@ public boolean equals(Object o) { if (attachments != null ? !attachments.equals(that.attachments) : that.attachments != null) return false; if (groupSummary != that.groupSummary) return false; + if( ongoing != that.ongoing ) return false; + if( autoCancel != that.autoCancel ) return false; return schedule != null ? schedule.equals(that.schedule) : that.schedule == null; } @@ -325,6 +349,8 @@ public int hashCode() { result = 31 * result + (actionTypeId != null ? actionTypeId.hashCode() : 0); result = 31 * result + (group != null ? group.hashCode() : 0); result = 31 * result + Boolean.hashCode(groupSummary); + result = 31 * result + Boolean.hashCode( ongoing ); + result = 31 * result + Boolean.hashCode( autoCancel ); result = 31 * result + (extra != null ? extra.hashCode() : 0); result = 31 * result + (attachments != null ? attachments.hashCode() : 0); result = 31 * result + (schedule != null ? schedule.hashCode() : 0); diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index c0675465e..7d5e461ae 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -174,8 +174,8 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, channelId) .setContentTitle(localNotification.getTitle()) .setContentText(localNotification.getBody()) - .setAutoCancel(true) - .setOngoing(false) + .setAutoCancel( localNotification.isAutoCancel( ) ) + .setOngoing( localNotification.isOngoing( ) ) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setGroupSummary(localNotification.isGroupSummary()); diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 24fc68b19..c9b6a6efa 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1144,6 +1144,15 @@ export interface LocalNotification { * notification will not fire. If not provided, it will use the default channel. */ channelId?: string; + /** + * Android only: set the notification ongoing. + * If set to true the notification can't be swiped away. + */ + ongoing?: boolean; + /** + * Android only: set the notification to be removed automatically when the user clicks on it + */ + autoCancel?: boolean; } export interface LocalNotificationSchedule {