Skip to content

Commit

Permalink
feat(local-notifications): add more info to pending notifications (#211)
Browse files Browse the repository at this point in the history
Co-authored-by: Joseph Pender <[email protected]>
  • Loading branch information
theproducer and Joseph Pender authored Feb 5, 2021
1 parent 43380ef commit 7c50487
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 22 deletions.
17 changes: 14 additions & 3 deletions local-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,20 @@ Represents a notification attachment.

#### PendingResult

| Prop | Type | Description | Since |
| ------------------- | ------------------------------------------ | ---------------------------------- | ----- |
| **`notifications`** | <code>LocalNotificationDescriptor[]</code> | The list of pending notifications. | 1.0.0 |
| Prop | Type | Description | Since |
| ------------------- | --------------------------------------------- | ---------------------------------- | ----- |
| **`notifications`** | <code>PendingLocalNotificationSchema[]</code> | The list of pending notifications. | 1.0.0 |


#### PendingLocalNotificationSchema

| Prop | Type | Description | Since |
| -------------- | --------------------------------------------- | -------------------------------------------------------------------- | ----- |
| **`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 |
| **`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 |
| **`extra`** | <code>any</code> | Set extra data to store within this notification. | 1.0.0 |


#### RegisterActionTypesOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,28 @@ public static List<Integer> getLocalNotificationPendingList(PluginCall call) {
return notificationsList;
}

public static JSObject buildLocalNotificationPendingList(List<String> ids) {
public static JSObject buildLocalNotificationPendingList(List<LocalNotification> notifications) {
JSObject result = new JSObject();
JSArray jsArray = new JSArray();
for (String id : ids) {
JSObject notification = new JSObject();
try {
int intId = Integer.parseInt(id);
notification.put("id", intId);
} catch (NumberFormatException ex) {
notification.put("id", -1);
for (LocalNotification notification : notifications) {
JSObject jsNotification = new JSObject();
jsNotification.put("id", notification.getId());
jsNotification.put("title", notification.getTitle());
jsNotification.put("body", notification.getBody());
LocalNotificationSchedule schedule = notification.getSchedule();
if (schedule != null) {
JSObject jsSchedule = new JSObject();
jsSchedule.put("at", schedule.getAt());
jsSchedule.put("every", schedule.getEvery());
jsSchedule.put("count", schedule.getCount());
jsSchedule.put("on", schedule.getOn());
jsSchedule.put("repeats", schedule.isRepeating());
jsNotification.put("schedule", jsSchedule);
}
jsArray.put(notification);

jsNotification.put("extra", notification.getExtra());

jsArray.put(jsNotification);
}
result.put("notifications", jsArray);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public void cancel(PluginCall call) {

@PluginMethod
public void getPending(PluginCall call) {
List<String> ids = notificationStorage.getSavedNotificationIds();
JSObject result = LocalNotification.buildLocalNotificationPendingList(ids);
List<LocalNotification> notifications = notificationStorage.getSavedNotifications();
JSObject result = LocalNotification.buildLocalNotificationPendingList(notifications);
call.resolve(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,43 @@ public List<String> getSavedNotificationIds() {
return new ArrayList<>();
}

public List<LocalNotification> getSavedNotifications() {
SharedPreferences storage = getStorage(NOTIFICATION_STORE_ID);
Map<String, ?> all = storage.getAll();
if (all != null) {
ArrayList<LocalNotification> notifications = new ArrayList<>();
for (String key : all.keySet()) {
String notificationString = (String) all.get(key);
JSObject jsNotification = getNotificationFromJSONString(notificationString);
if (jsNotification != null) {
try {
LocalNotification notification = LocalNotification.buildNotificationFromJSObject(jsNotification);
notifications.add(notification);
} catch (ParseException ex) {}
}
}

return notifications;
}

return new ArrayList<>();
}

public JSObject getNotificationFromJSONString(String notificationString) {
if (notificationString == null) {
return null;
}

JSObject jsNotification;
try {
jsNotification = new JSObject(notificationString);
} catch (JSONException ex) {
return null;
}

return jsNotification;
}

public JSObject getSavedNotificationAsJSObject(String key) {
SharedPreferences storage = getStorage(NOTIFICATION_STORE_ID);
String notificationString = storage.getString(key, null);
Expand Down
41 changes: 38 additions & 3 deletions local-notifications/ios/Plugin/LocalNotificationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public class LocalNotificationsPlugin: CAPPlugin {
let ret = notifications.compactMap({ [weak self] (notification) -> JSObject? in
return self?.makePendingNotificationRequestJSObject(notification)
})

call.resolve([
"notifications": ret
])
Expand Down Expand Up @@ -206,12 +207,17 @@ public class LocalNotificationsPlugin: CAPPlugin {
}

let extra = notification["extra"] as? JSObject ?? [:]
let schedule = notification["schedule"] as? JSObject ?? [:]
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: title, arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: body,
arguments: nil)

content.userInfo = extra
content.userInfo = [
"cap_extra": extra,
"cap_schedule": schedule
]

if let actionTypeId = notification["actionTypeId"] as? String {
content.categoryIdentifier = actionTypeId
}
Expand Down Expand Up @@ -540,9 +546,38 @@ public class LocalNotificationsPlugin: CAPPlugin {
}

func makePendingNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {
return [
"id": Int(request.identifier) ?? -1
var notification: JSObject = [
"id": Int(request.identifier) ?? -1,
"title": request.content.title,
"body": request.content.body
]

if let userInfo = JSTypes.coerceDictionaryToJSObject(request.content.userInfo) {
var extra = userInfo["cap_extra"] as? JSObject ?? userInfo

// check for any dates and convert them to strings
for(key, value) in extra {
if let date = value as? Date {
let dateString = ISO8601DateFormatter().string(from: date)
extra[key] = dateString
}
}

notification["extra"] = extra

if var schedule = userInfo["cap_schedule"] as? JSObject {
// convert schedule at date to string
if let date = schedule["at"] as? Date {
let dateString = ISO8601DateFormatter().string(from: date)
schedule["at"] = dateString
}

notification["schedule"] = schedule
}
}

return notification

}

@objc func createChannel(_ call: CAPPluginCall) {
Expand Down
39 changes: 38 additions & 1 deletion local-notifications/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export interface PendingResult {
*
* @since 1.0.0
*/
notifications: LocalNotificationDescriptor[];
notifications: PendingLocalNotificationSchema[];
}

export interface RegisterActionTypesOptions {
Expand Down Expand Up @@ -442,6 +442,43 @@ export interface AttachmentOptions {
iosUNNotificationAttachmentOptionsThumbnailTimeKey?: string;
}

export interface PendingLocalNotificationSchema {
/**
* The title of the notification.
*
* @since 1.0.0
*/
title: string;

/**
* The body of the notification, shown below the title.
*
* @since 1.0.0
*/
body: string;

/**
* The notification identifier.
*
* @since 1.0.0
*/
id: number;

/**
* Schedule this notification for a later time.
*
* @since 1.0.0
*/
schedule?: Schedule;

/**
* Set extra data to store within this notification.
*
* @since 1.0.0
*/
extra?: any;
}

export interface LocalNotificationSchema {
/**
* The title of the notification.
Expand Down
7 changes: 3 additions & 4 deletions local-notifications/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ListChannelsResult,
LocalNotificationSchema,
LocalNotificationsPlugin,
PendingResult,
PermissionStatus,
ScheduleOptions,
ScheduleResult,
Expand Down Expand Up @@ -40,11 +41,9 @@ export class LocalNotificationsWeb
};
}

async getPending(): Promise<ScheduleResult> {
async getPending(): Promise<PendingResult> {
return {
notifications: this.pending.map(notification => ({
id: notification.id,
})),
notifications: this.pending,
};
}

Expand Down

0 comments on commit 7c50487

Please sign in to comment.