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 d830f7f46..469e37812 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 @@ -3,6 +3,7 @@ import android.content.Context; import android.util.Log; +import com.getcapacitor.Config; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.LogUtils; @@ -21,10 +22,15 @@ */ public class LocalNotification { + private static final String CONFIG_KEY_PREFIX = "plugins.LocalNotifications."; + private static final int RESOURCE_ID_ZERO_VALUE = 0; + private static int defaultSmallIconID = RESOURCE_ID_ZERO_VALUE; + private String title; private String body; private Integer id; private String sound; + private String smallIcon; private String actionTypeId; private JSObject extra; private List attachments; @@ -65,6 +71,7 @@ public void setSound(String sound) { this.sound = sound; } + public void setSmallIcon(String smallIcon) { this.smallIcon = getResourceBaseName(smallIcon); } public List getAttachments() { return attachments; @@ -131,6 +138,7 @@ public static List buildNotificationList(PluginCall call) { activeLocalNotification.setActionTypeId(notification.getString("actionTypeId")); activeLocalNotification.setSound(notification.getString("sound")); activeLocalNotification.setTitle(notification.getString("title")); + activeLocalNotification.setSmallIcon(notification.getString("smallIcon")); activeLocalNotification.setAttachments(LocalNotificationAttachment.getAttachments(notification)); try { activeLocalNotification.setSchedule(new LocalNotificationSchedule(notification)); @@ -178,8 +186,35 @@ public static JSObject buildLocalNotificationPendingList(List ids) { } public int getSmallIcon(Context context) { - // TODO support custom icons - int resId = android.R.drawable.ic_dialog_info; + int resId = RESOURCE_ID_ZERO_VALUE; + + if(smallIcon != null){ + resId = getResourceID(context, smallIcon,"drawable"); + } + + if(resId == RESOURCE_ID_ZERO_VALUE){ + resId = getDefaultSmallIcon(context); + } + + return resId; + } + + private static int getDefaultSmallIcon(Context context){ + if(defaultSmallIconID != RESOURCE_ID_ZERO_VALUE) return defaultSmallIconID; + + int resId = RESOURCE_ID_ZERO_VALUE; + String smallIconConfigResourceName = Config.getString(CONFIG_KEY_PREFIX + "smallIcon"); + smallIconConfigResourceName = getResourceBaseName(smallIconConfigResourceName); + + if(smallIconConfigResourceName != null){ + resId = getResourceID(context, smallIconConfigResourceName, "drawable"); + } + + if(resId == RESOURCE_ID_ZERO_VALUE){ + resId = android.R.drawable.ic_dialog_info; + } + + defaultSmallIconID = resId; return resId; } @@ -197,6 +232,7 @@ public String toString() { ", body='" + body + '\'' + ", id=" + id + ", sound='" + sound + '\'' + + ", smallIcon='" + smallIcon + '\'' + ", actionTypeId='" + actionTypeId + '\'' + ", extra=" + extra + ", attachments=" + attachments + @@ -215,6 +251,7 @@ public boolean equals(Object o) { if (body != null ? !body.equals(that.body) : that.body != null) return false; 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 (actionTypeId != null ? !actionTypeId.equals(that.actionTypeId) : that.actionTypeId != null) return false; if (extra != null ? !extra.equals(that.extra) : that.extra != null) return false; @@ -229,6 +266,7 @@ public int hashCode() { result = 31 * result + (body != null ? body.hashCode() : 0); 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 + (actionTypeId != null ? actionTypeId.hashCode() : 0); result = 31 * result + (extra != null ? extra.hashCode() : 0); result = 31 * result + (attachments != null ? attachments.hashCode() : 0); @@ -253,4 +291,22 @@ public String getSource() { public void setSource(String source) { this.source = source; } + + private static int getResourceID(Context context, String resourceName, String dir){ + return context.getResources().getIdentifier(resourceName, dir, context.getPackageName()); + } + + private static String getResourceBaseName (String resPath) { + if (resPath == null) return null; + + if (resPath.contains("/")) { + return resPath.substring(resPath.lastIndexOf('/') + 1); + } + + if (resPath.contains(".")) { + return resPath.substring(0, resPath.lastIndexOf('.')); + } + + return resPath; + } } diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index b004065d3..6329b71d6 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -945,6 +945,11 @@ export interface LocalNotification { id: number; schedule?: LocalNotificationSchedule; sound?: string; + /** + * Android-only: set a custom statusbar icon. + * If set, it overrides default icon from capacitor.config.json + */ + smallIcon?: string; attachments?: LocalNotificationAttachment[]; actionTypeId?: string; extra?: any; diff --git a/example/android/app/src/main/res/drawable/ic_stat_icon_config_sample.png b/example/android/app/src/main/res/drawable/ic_stat_icon_config_sample.png new file mode 100644 index 000000000..7bcd81be5 Binary files /dev/null and b/example/android/app/src/main/res/drawable/ic_stat_icon_config_sample.png differ diff --git a/example/android/app/src/main/res/drawable/ic_stat_icon_sample.png b/example/android/app/src/main/res/drawable/ic_stat_icon_sample.png new file mode 100644 index 000000000..458abd50e Binary files /dev/null and b/example/android/app/src/main/res/drawable/ic_stat_icon_sample.png differ diff --git a/example/capacitor.config.json b/example/capacitor.config.json index c46c43283..7d42eec98 100644 --- a/example/capacitor.config.json +++ b/example/capacitor.config.json @@ -6,6 +6,9 @@ "plugins": { "SplashScreen": { "launchShowDuration": 12345 + }, + "LocalNotifications": { + "smallIcon": "ic_stat_icon_config_sample" } } -} +} \ 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 3cebdc244..3f2cf4bd0 100644 --- a/example/src/pages/local-notifications/local-notifications.html +++ b/example/src/pages/local-notifications/local-notifications.html @@ -19,7 +19,10 @@

+