Skip to content

Commit

Permalink
feat(android): support custom local notification icon (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis authored and jcesarmobile committed Aug 1, 2019
1 parent cda0999 commit f7c42db
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<LocalNotificationAttachment> attachments;
Expand Down Expand Up @@ -65,6 +71,7 @@ public void setSound(String sound) {
this.sound = sound;
}

public void setSmallIcon(String smallIcon) { this.smallIcon = getResourceBaseName(smallIcon); }

public List<LocalNotificationAttachment> getAttachments() {
return attachments;
Expand Down Expand Up @@ -131,6 +138,7 @@ public static List<LocalNotification> 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));
Expand Down Expand Up @@ -178,8 +186,35 @@ public static JSObject buildLocalNotificationPendingList(List<String> 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;
}

Expand All @@ -197,6 +232,7 @@ public String toString() {
", body='" + body + '\'' +
", id=" + id +
", sound='" + sound + '\'' +
", smallIcon='" + smallIcon + '\'' +
", actionTypeId='" + actionTypeId + '\'' +
", extra=" + extra +
", attachments=" + attachments +
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
}
5 changes: 5 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion example/capacitor.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"plugins": {
"SplashScreen": {
"launchShowDuration": 12345
},
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
</button>
<br/><br/>
<button (click)="scheduleNow()" ion-button>
Schedule Now
Schedule Now - Icon from config (or default)
</button>
<button (click)="scheduleNowWithIcon()" ion-button>
Schedule Now - Icon parameter
</button>
<br/><br/>
<button (click)="scheduleOnce()" ion-button>
Expand Down
21 changes: 21 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,27 @@ export class LocalNotificationsPage {
});
}

async scheduleNowWithIcon() {
this.notifs = await Plugins.LocalNotifications.schedule({
notifications: [{
title: 'Get 10% off!',
body: 'Swipe now to learn more',
// Android-only: set a custom statusbar icon
smallIcon: "res://ic_stat_icon_sample",
// 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'
}
}]
});
}

async scheduleOnce() {
var now = new Date();
this.notifs = await Plugins.LocalNotifications.schedule({
Expand Down

0 comments on commit f7c42db

Please sign in to comment.