Skip to content

Commit

Permalink
feat(local-notifications): Support for Big Text and Inbox Notificatio…
Browse files Browse the repository at this point in the history
…n Style (#280)

* Adding support for largeIcon and notification Inbox style

* Adding explicit support for big text style notifications and notification summaryText
  • Loading branch information
theproducer authored Mar 3, 2021
1 parent a90a88b commit dc96ef9
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
4 changes: 4 additions & 0 deletions local-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,13 @@ The object that describes a local notification.
| ---------------------- | --------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`title`** | <code>string</code> | The title of the notification. | 1.0.0 |
| **`body`** | <code>string</code> | The body of the notification, shown below the title. | 1.0.0 |
| **`largeBody`** | <code>string</code> | Sets a multiline text block for display in a big text notification style. | 1.0.0 |
| **`summaryText`** | <code>string</code> | Used to set the summary text detail in inbox and big text notification styles. Only available for Android. | 1.0.0 |
| **`id`** | <code>number</code> | The notification identifier. | 1.0.0 |
| **`schedule`** | <code><a href="#schedule">Schedule</a></code> | <a href="#schedule">Schedule</a> this notification for a later time. | 1.0.0 |
| **`sound`** | <code>string</code> | Name of the audio file to play when this notification is displayed. Include the file extension with the filename. On iOS, the file should be in the app bundle. On Android, the file should be in res/raw folder. Recommended format is `.wav` because is supported by both iOS and Android. Only available for iOS and Android 26+. | 1.0.0 |
| **`smallIcon`** | <code>string</code> | Set a custom status bar icon. If set, this overrides the `smallIcon` option from Capacitor configuration. Icons should be placed in your app's `res/drawable` folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android. | 1.0.0 |
| **`largeIcon`** | <code>string</code> | Set a large icon for notifications. Icons should be placed in your app's `res/drawable` folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android. | 1.0.0 |
| **`iconColor`** | <code>string</code> | Set the color of the notification icon. Only available for Android. | 1.0.0 |
| **`attachments`** | <code>Attachment[]</code> | Set attachments for this notification. | 1.0.0 |
| **`actionTypeId`** | <code>string</code> | Associate an action type with this notification. | 1.0.0 |
Expand All @@ -328,6 +331,7 @@ The object that describes a local notification.
| **`channelId`** | <code>string</code> | Specifies the channel the notification should be delivered on. If channel with the given name does not exist then the notification will not fire. If not provided, it will use the default channel. Calls `setChannelId()` on [`NotificationCompat.Builder`](https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder) with the provided value. Only available for Android 26+. | 1.0.0 |
| **`ongoing`** | <code>boolean</code> | If true, the notification can't be swiped away. Calls `setOngoing()` on [`NotificationCompat.Builder`](https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder) with the provided value. Only available for Android. | 1.0.0 |
| **`autoCancel`** | <code>boolean</code> | If true, the notification is canceled when the user clicks on it. Calls `setAutoCancel()` on [`NotificationCompat.Builder`](https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder) with the provided value. Only available for Android. | 1.0.0 |
| **`inboxList`** | <code>string[]</code> | Sets a list of strings for display in an inbox style notification. Up to 5 strings are allowed. Only available for Android. | 1.0.0 |


#### Schedule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
Expand All @@ -10,6 +12,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -20,12 +23,16 @@ public class LocalNotification {

private String title;
private String body;
private String largeBody;
private String summaryText;
private Integer id;
private String sound;
private String smallIcon;
private String largeIcon;
private String iconColor;
private String actionTypeId;
private String group;
private List<String> inboxList;
private boolean groupSummary;
private boolean ongoing;
private boolean autoCancel;
Expand All @@ -51,6 +58,22 @@ public void setBody(String body) {
this.body = body;
}

public void setLargeBody(String largeBody) {
this.largeBody = largeBody;
}

public String getLargeBody() {
return this.largeBody;
}

public void setSummaryText(String summaryText) {
this.summaryText = summaryText;
}

public String getSummaryText() {
return this.summaryText;
}

public LocalNotificationSchedule getSchedule() {
return schedule;
}
Expand Down Expand Up @@ -83,6 +106,18 @@ public void setSmallIcon(String smallIcon) {
this.smallIcon = AssetUtil.getResourceBaseName(smallIcon);
}

public void setLargeIcon(String largeIcon) {
this.largeIcon = AssetUtil.getResourceBaseName(largeIcon);
}

public void setInboxList(List<String> inboxList) {
this.inboxList = inboxList;
}

public List<String> getInboxList() {
return this.inboxList;
}

public String getIconColor(String globalColor) {
// use the one defined local before trying for a globally defined color
if (iconColor != null) {
Expand Down Expand Up @@ -211,25 +246,37 @@ public static LocalNotification buildNotificationFromJSObject(JSObject jsonObjec
localNotification.setSource(jsonObject.toString());
localNotification.setId(jsonObject.getInteger("id"));
localNotification.setBody(jsonObject.getString("body"));
localNotification.setLargeBody(jsonObject.getString("largeBody"));
localNotification.setSummaryText(jsonObject.getString("summaryText"));
localNotification.setActionTypeId(jsonObject.getString("actionTypeId"));
localNotification.setGroup(jsonObject.getString("group"));
localNotification.setSound(jsonObject.getString("sound"));
localNotification.setTitle(jsonObject.getString("title"));
localNotification.setSmallIcon(jsonObject.getString("smallIcon"));
localNotification.setLargeIcon(jsonObject.getString("largeIcon"));
localNotification.setIconColor(jsonObject.getString("iconColor"));
localNotification.setAttachments(LocalNotificationAttachment.getAttachments(jsonObject));
localNotification.setGroupSummary(jsonObject.getBoolean("groupSummary", false));
localNotification.setChannelId(jsonObject.getString("channelId"));

JSObject schedule = jsonObject.getJSObject("schedule");
if (schedule != null) {
localNotification.setSchedule(new LocalNotificationSchedule(schedule));
}

localNotification.setExtra(jsonObject.getJSObject("extra"));
localNotification.setOngoing(jsonObject.getBoolean("ongoing", false));
localNotification.setAutoCancel(jsonObject.getBoolean("autoCancel", true));

try {
JSONArray inboxList = jsonObject.getJSONArray("inboxList");
if (inboxList != null) {
List<String> inboxStringList = new ArrayList<>();
for (int i = 0; i < inboxList.length(); i++) {
inboxStringList.add(inboxList.getString(i));
}
localNotification.setInboxList(inboxStringList);
}
} catch (Exception ex) {}

return localNotification;
}

Expand Down Expand Up @@ -292,6 +339,15 @@ public int getSmallIcon(Context context, int defaultIcon) {
return resId;
}

public Bitmap getLargeIcon(Context context) {
if (largeIcon != null) {
int resId = AssetUtil.getResourceID(context, largeIcon, "drawable");
return BitmapFactory.decodeResource(context.getResources(), resId);
}

return null;
}

public boolean isScheduled() {
return (
this.schedule != null && (this.schedule.getOn() != null || this.schedule.getAt() != null || this.schedule.getEvery() != null)
Expand Down Expand Up @@ -350,14 +406,17 @@ public boolean equals(Object o) {

if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (body != null ? !body.equals(that.body) : that.body != null) return false;
if (largeBody != null ? !largeBody.equals(that.largeBody) : that.largeBody != 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 (largeIcon != null ? !largeIcon.equals(that.largeIcon) : that.largeIcon != 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;
if (extra != null ? !extra.equals(that.extra) : that.extra != null) return false;
if (attachments != null ? !attachments.equals(that.attachments) : that.attachments != null) return false;
if (inboxList != null ? !inboxList.equals(that.inboxList) : that.inboxList != null) return false;
if (groupSummary != that.groupSummary) return false;
if (ongoing != that.ongoing) return false;
if (autoCancel != that.autoCancel) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,24 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroupSummary(localNotification.isGroupSummary());

// support multiline text
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(localNotification.getBody()));
if (localNotification.getLargeBody() != null) {
// support multiline text
mBuilder.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(localNotification.getLargeBody())
.setSummaryText(localNotification.getSummaryText())
);
}

if (localNotification.getInboxList() != null) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for (String line : localNotification.getInboxList()) {
inboxStyle.addLine(line);
}
inboxStyle.setBigContentTitle(localNotification.getTitle());
inboxStyle.setSummaryText(localNotification.getSummaryText());
mBuilder.setStyle(inboxStyle);
}

String sound = localNotification.getSound(context, getDefaultSound(context));
if (sound != null) {
Expand All @@ -202,6 +218,7 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo
mBuilder.setOnlyAlertOnce(true);

mBuilder.setSmallIcon(localNotification.getSmallIcon(context, getDefaultSmallIcon(context)));
mBuilder.setLargeIcon(localNotification.getLargeIcon(context));

String iconColor = localNotification.getIconColor(config.getString(CONFIG_KEY_PREFIX + "iconColor"));
if (iconColor != null) {
Expand Down
39 changes: 39 additions & 0 deletions local-notifications/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@ export interface LocalNotificationSchema {
*/
body: string;

/**
* Sets a multiline text block for display in a big text notification style.
*
* @since 1.0.0
*/
largeBody?: string;

/**
* Used to set the summary text detail in inbox and big text notification styles.
*
* Only available for Android.
*
* @since 1.0.0
*/
summaryText?: string;
/**
* The notification identifier.
*
Expand Down Expand Up @@ -540,6 +555,19 @@ export interface LocalNotificationSchema {
*/
smallIcon?: string;

/**
* Set a large icon for notifications.
*
* Icons should be placed in your app's `res/drawable` folder. The value for
* this option should be the drawable resource ID, which is the filename
* without an extension.
*
* Only available for Android.
*
* @since 1.0.0
*/
largeIcon?: string;

/**
* Set the color of the notification icon.
*
Expand Down Expand Up @@ -662,6 +690,17 @@ export interface LocalNotificationSchema {
* @since 1.0.0
*/
autoCancel?: boolean;

/**
* Sets a list of strings for display in an inbox style notification.
*
* Up to 5 strings are allowed.
*
* Only available for Android.
*
* @since 1.0.0
*/
inboxList?: string[];
}

/**
Expand Down

0 comments on commit dc96ef9

Please sign in to comment.