Skip to content

Commit 1ee51cd

Browse files
authored
feat(android): add option to make a notification ongoing (#3165)
1 parent 2a39a7d commit 1ee51cd

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class LocalNotification {
3333
private String actionTypeId;
3434
private String group;
3535
private boolean groupSummary;
36+
private boolean ongoing;
37+
private boolean autoCancel;
3638
private JSObject extra;
3739
private List<LocalNotificationAttachment> attachments;
3840
private LocalNotificationSchedule schedule;
@@ -152,6 +154,22 @@ public void setGroupSummary(boolean groupSummary) {
152154
this.groupSummary = groupSummary;
153155
}
154156

157+
public boolean isOngoing() {
158+
return ongoing;
159+
}
160+
161+
public void setOngoing(boolean ongoing) {
162+
this.ongoing = ongoing;
163+
}
164+
165+
public boolean isAutoCancel() {
166+
return autoCancel;
167+
}
168+
169+
public void setAutoCancel(boolean autoCancel) {
170+
this.autoCancel = autoCancel;
171+
}
172+
155173
public String getChannelId() {
156174
return channelId;
157175
}
@@ -186,7 +204,7 @@ public static List<LocalNotification> buildNotificationList(PluginCall call) {
186204
call.error("Invalid JSON object sent to NotificationPlugin", e);
187205
return null;
188206
}
189-
207+
190208
try {
191209
LocalNotification activeLocalNotification = buildNotificationFromJSObject(notification);
192210
resultLocalNotifications.add(activeLocalNotification);
@@ -214,6 +232,8 @@ public static LocalNotification buildNotificationFromJSObject(JSObject jsonObjec
214232
localNotification.setChannelId(jsonObject.getString("channelId"));
215233
localNotification.setSchedule(new LocalNotificationSchedule(jsonObject));
216234
localNotification.setExtra(jsonObject.getJSObject("extra"));
235+
localNotification.setOngoing(jsonObject.getBoolean("ongoing", false));
236+
localNotification.setAutoCancel(jsonObject.getBoolean("autoCancel", true));
217237

218238
return localNotification;
219239
}
@@ -288,6 +308,8 @@ public String toString() {
288308
", attachments=" + attachments +
289309
", schedule=" + schedule +
290310
", groupSummary=" + groupSummary +
311+
", ongoing=" + ongoing +
312+
", autoCancel=" + autoCancel +
291313
'}';
292314
}
293315

@@ -311,6 +333,8 @@ public boolean equals(Object o) {
311333
if (attachments != null ? !attachments.equals(that.attachments) : that.attachments != null)
312334
return false;
313335
if (groupSummary != that.groupSummary) return false;
336+
if( ongoing != that.ongoing ) return false;
337+
if( autoCancel != that.autoCancel ) return false;
314338
return schedule != null ? schedule.equals(that.schedule) : that.schedule == null;
315339
}
316340

@@ -325,6 +349,8 @@ public int hashCode() {
325349
result = 31 * result + (actionTypeId != null ? actionTypeId.hashCode() : 0);
326350
result = 31 * result + (group != null ? group.hashCode() : 0);
327351
result = 31 * result + Boolean.hashCode(groupSummary);
352+
result = 31 * result + Boolean.hashCode( ongoing );
353+
result = 31 * result + Boolean.hashCode( autoCancel );
328354
result = 31 * result + (extra != null ? extra.hashCode() : 0);
329355
result = 31 * result + (attachments != null ? attachments.hashCode() : 0);
330356
result = 31 * result + (schedule != null ? schedule.hashCode() : 0);

android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo
174174
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, channelId)
175175
.setContentTitle(localNotification.getTitle())
176176
.setContentText(localNotification.getBody())
177-
.setAutoCancel(true)
178-
.setOngoing(false)
177+
.setAutoCancel( localNotification.isAutoCancel( ) )
178+
.setOngoing( localNotification.isOngoing( ) )
179179
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
180180
.setGroupSummary(localNotification.isGroupSummary());
181181

core/src/core-plugin-definitions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,15 @@ export interface LocalNotification {
11441144
* notification will not fire. If not provided, it will use the default channel.
11451145
*/
11461146
channelId?: string;
1147+
/**
1148+
* Android only: set the notification ongoing.
1149+
* If set to true the notification can't be swiped away.
1150+
*/
1151+
ongoing?: boolean;
1152+
/**
1153+
* Android only: set the notification to be removed automatically when the user clicks on it
1154+
*/
1155+
autoCancel?: boolean;
11471156
}
11481157

11491158
export interface LocalNotificationSchedule {

0 commit comments

Comments
 (0)