From 648d8a1e512c02fab2dfbf4dbf6df19e03c1c3df Mon Sep 17 00:00:00 2001 From: Nikos Douvlis Date: Wed, 31 Jul 2019 02:55:29 +0300 Subject: [PATCH 1/3] feat(android): support custom local notification icon --- .../notification/LocalNotification.java | 33 ++++++++++++++++-- .../main/res/drawable/ic_stat_icon_sample.png | Bin 0 -> 2352 bytes core/src/core-plugin-definitions.ts | 5 +++ .../local-notifications.html | 5 ++- .../local-notifications.ts | 21 +++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 android/capacitor/src/main/res/drawable/ic_stat_icon_sample.png 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..305967fe2 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 @@ -7,6 +7,7 @@ import com.getcapacitor.JSObject; import com.getcapacitor.LogUtils; import com.getcapacitor.PluginCall; +import com.getcapacitor.android.R; import org.json.JSONException; import org.json.JSONObject; @@ -25,6 +26,7 @@ public class LocalNotification { private String body; private Integer id; private String sound; + private String smallIcon; private String actionTypeId; private JSObject extra; private List attachments; @@ -65,6 +67,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 +134,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 +182,16 @@ 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 = 0; + + if(smallIcon != null){ + resId = context.getResources().getIdentifier(smallIcon,"drawable", context.getPackageName()); + } + + if(resId == 0){ + resId = android.R.drawable.ic_dialog_info; + } + return resId; } @@ -197,6 +209,7 @@ public String toString() { ", body='" + body + '\'' + ", id=" + id + ", sound='" + sound + '\'' + + ", smallIcon='" + smallIcon + '\'' + ", actionTypeId='" + actionTypeId + '\'' + ", extra=" + extra + ", attachments=" + attachments + @@ -215,6 +228,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 +243,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 +268,18 @@ public String getSource() { public void setSource(String source) { this.source = source; } + + private 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/android/capacitor/src/main/res/drawable/ic_stat_icon_sample.png b/android/capacitor/src/main/res/drawable/ic_stat_icon_sample.png new file mode 100644 index 0000000000000000000000000000000000000000..458abd50eed0e11bb0cf2fb54ec860540239004a GIT binary patch literal 2352 zcmV-03D5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