Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): added configurable icon color for local notifications #2548

Merged
merged 4 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -174,6 +175,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 (Exception ex) {
call.error("The iconColor string was not able to be parsed. Please provide a valid string hexidecimal color code.", ex);
andysousa marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1033,6 +1033,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
4 changes: 3 additions & 1 deletion site/docs-md/apis/local-notifications/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ LocalNotifications.schedule({
sound: null,
attachments: null,
actionTypeId: "",
extra: null
extra: null,
smallIcon: null,
iconColor: null
andysousa marked this conversation as resolved.
Show resolved Hide resolved
}
]
});
Expand Down