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 7df50102f5..3028097516 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 @@ -31,6 +31,7 @@ public class LocalNotification { private Integer id; private String sound; private String smallIcon; + private String iconColor; private String actionTypeId; private String group; private boolean groupSummary; @@ -75,6 +76,24 @@ public void setSound(String sound) { public void setSmallIcon(String smallIcon) { this.smallIcon = getResourceBaseName(smallIcon); } + public String getIconColor() { + // use the one defined local before trying for a globally defined color + if (iconColor != null) { + return iconColor; + } + + String globalColor = Config.getString(CONFIG_KEY_PREFIX + "iconColor"); + if (globalColor != null) { + return globalColor; + } + + return null; + } + + public void setIconColor(String iconColor) { + this.iconColor = iconColor; + } + public List getAttachments() { return attachments; } @@ -158,6 +177,7 @@ public static List buildNotificationList(PluginCall call) { activeLocalNotification.setSound(notification.getString("sound")); activeLocalNotification.setTitle(notification.getString("title")); activeLocalNotification.setSmallIcon(notification.getString("smallIcon")); + activeLocalNotification.setIconColor(notification.getString("iconColor")); activeLocalNotification.setAttachments(LocalNotificationAttachment.getAttachments(notification)); activeLocalNotification.setGroupSummary(notification.getBoolean("groupSummary", false)); try { @@ -253,6 +273,7 @@ public String toString() { ", id=" + id + ", sound='" + sound + '\'' + ", smallIcon='" + smallIcon + '\'' + + ", iconColor='" + iconColor + '\'' + ", actionTypeId='" + actionTypeId + '\'' + ", group='" + group + '\'' + ", extra=" + extra + @@ -274,6 +295,7 @@ public boolean equals(Object o) { if (id != null ? !id.equals(that.id) : that.id != null) return false; if (sound != null ? !sound.equals(that.sound) : that.sound != null) return false; if (smallIcon != null ? !smallIcon.equals(that.smallIcon) : that.smallIcon != null) return false; + if (iconColor != null ? !iconColor.equals(that.iconColor) : that.iconColor != null) return false; if (actionTypeId != null ? !actionTypeId.equals(that.actionTypeId) : that.actionTypeId != null) return false; if (group != null ? !group.equals(that.group) : that.group != null) return false; @@ -291,6 +313,7 @@ public int hashCode() { result = 31 * result + (id != null ? id.hashCode() : 0); result = 31 * result + (sound != null ? sound.hashCode() : 0); result = 31 * result + (smallIcon != null ? smallIcon.hashCode() : 0); + result = 31 * result + (iconColor != null ? iconColor.hashCode() : 0); result = 31 * result + (actionTypeId != null ? actionTypeId.hashCode() : 0); result = 31 * result + (group != null ? group.hashCode() : 0); result = 31 * result + Boolean.hashCode(groupSummary); 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 b7be3161d7..202de9d80e 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 @@ -7,6 +7,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.graphics.Color; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -184,6 +185,17 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo mBuilder.setOnlyAlertOnce(true); mBuilder.setSmallIcon(localNotification.getSmallIcon(context)); + + String iconColor = localNotification.getIconColor(); + if (iconColor != null) { + try { + mBuilder.setColor(Color.parseColor(iconColor)); + } catch (IllegalArgumentException ex) { + call.error("Invalid color provided. Must be a hex string (ex: #ff0000"); + return; + } + } + createActionIntents(localNotification, mBuilder); // notificationId is a unique int for each localNotification that you must define Notification buildNotification = mBuilder.build(); diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 56149ab40a..9d6fbd0095 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1034,6 +1034,10 @@ export interface LocalNotification { * If set, it overrides default icon from capacitor.config.json */ smallIcon?: string; + /** + * Android only: set the color of the notification icon + */ + iconColor?: string attachments?: LocalNotificationAttachment[]; actionTypeId?: string; extra?: any; diff --git a/example/android/app/src/main/assets/capacitor.config.json b/example/android/app/src/main/assets/capacitor.config.json index 5bd0006f90..f3d2ba2ac9 100644 --- a/example/android/app/src/main/assets/capacitor.config.json +++ b/example/android/app/src/main/assets/capacitor.config.json @@ -6,6 +6,10 @@ "plugins": { "SplashScreen": { "launchShowDuration": 12345 + }, + "LocalNotifications": { + "smallIcon": "ic_stat_icon_config_sample", + "iconColor": "#CE0B7C" } } } \ No newline at end of file diff --git a/example/capacitor.config.json b/example/capacitor.config.json index 7d42eec986..f3d2ba2ac9 100644 --- a/example/capacitor.config.json +++ b/example/capacitor.config.json @@ -8,7 +8,8 @@ "launchShowDuration": 12345 }, "LocalNotifications": { - "smallIcon": "ic_stat_icon_config_sample" + "smallIcon": "ic_stat_icon_config_sample", + "iconColor": "#CE0B7C" } } } \ No newline at end of file diff --git a/example/ios/IonicRunner/IonicRunner/capacitor.config.json b/example/ios/IonicRunner/IonicRunner/capacitor.config.json index 7d42eec986..f3d2ba2ac9 100644 --- a/example/ios/IonicRunner/IonicRunner/capacitor.config.json +++ b/example/ios/IonicRunner/IonicRunner/capacitor.config.json @@ -8,7 +8,8 @@ "launchShowDuration": 12345 }, "LocalNotifications": { - "smallIcon": "ic_stat_icon_config_sample" + "smallIcon": "ic_stat_icon_config_sample", + "iconColor": "#CE0B7C" } } } \ No newline at end of file diff --git a/example/src/pages/local-notifications/local-notifications.html b/example/src/pages/local-notifications/local-notifications.html index 05014c7345..6b6920a483 100644 --- a/example/src/pages/local-notifications/local-notifications.html +++ b/example/src/pages/local-notifications/local-notifications.html @@ -24,6 +24,9 @@ +