Skip to content

Commit

Permalink
feat(android): add configurable icon color for local notifications (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andysousa authored Mar 17, 2020
1 parent f6c2288 commit 0bfa0bf
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<LocalNotificationAttachment> getAttachments() {
return attachments;
}
Expand Down Expand Up @@ -158,6 +177,7 @@ public static List<LocalNotification> 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 {
Expand Down Expand Up @@ -253,6 +273,7 @@ public String toString() {
", id=" + id +
", sound='" + sound + '\'' +
", smallIcon='" + smallIcon + '\'' +
", iconColor='" + iconColor + '\'' +
", actionTypeId='" + actionTypeId + '\'' +
", group='" + group + '\'' +
", extra=" + extra +
Expand All @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions example/android/app/src/main/assets/capacitor.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"plugins": {
"SplashScreen": {
"launchShowDuration": 12345
},
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#CE0B7C"
}
}
}
3 changes: 2 additions & 1 deletion example/capacitor.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"launchShowDuration": 12345
},
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample"
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#CE0B7C"
}
}
}
3 changes: 2 additions & 1 deletion example/ios/IonicRunner/IonicRunner/capacitor.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"launchShowDuration": 12345
},
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample"
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#CE0B7C"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<button (click)="scheduleNowWithIcon()" ion-button>
Schedule Now - Icon parameter
</button>
<button (click)="scheduleNowWithColor()" ion-button>
Schedule Now - Custom color
</button>
<br/><br/>
<button (click)="scheduleOnce()" ion-button>
Schedule in 10 seconds
Expand Down
20 changes: 20 additions & 0 deletions example/src/pages/local-notifications/local-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ export class LocalNotificationsPage {
});
}

async scheduleNowWithColor() {
this.notifs = await Plugins.LocalNotifications.schedule({
notifications: [{
title: 'Get 10% off!',
body: 'Swipe now to learn more',
// Get random id to test cancel
id: Math.floor(Math.random()*10),
sound: 'beep.aiff',
attachments: [
{ id: 'face', url: 'res://public/assets/ionitron.png' }
],
actionTypeId: 'OPEN_PRODUCT',
extra: {
productId: 'PRODUCT-1'
},
iconColor: '#00ff00'
}]
});
}

async scheduleNowWithIcon() {
this.notifs = await Plugins.LocalNotifications.schedule({
notifications: [{
Expand Down

0 comments on commit 0bfa0bf

Please sign in to comment.