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

fix: normalize use of integers for notification IDs #195

Merged
merged 11 commits into from
Jan 28, 2021
2 changes: 1 addition & 1 deletion local-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ The object that describes a local notification.

| Prop | Type | Description | Since |
| -------- | ------------------- | ---------------------------- | ----- |
| **`id`** | <code>string</code> | The notification identifier. | 1.0.0 |
| **`id`** | <code>number</code> | The notification identifier. | 1.0.0 |


#### ScheduleOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ public static JSObject buildLocalNotificationPendingList(List<String> ids) {
JSArray jsArray = new JSArray();
for (String id : ids) {
JSObject notification = new JSObject();
notification.put("id", id);
try {
int intId = Integer.parseInt(id);
notification.put("id", intId);
} catch (NumberFormatException ex) {
notification.put("id", -1);
}
jsArray.put(notification);
}
result.put("notifications", jsArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void schedule(PluginCall call) {
JSArray jsArray = new JSArray();
for (int i = 0; i < ids.length(); i++) {
try {
JSObject notification = new JSObject().put("id", ids.getString(i));
JSObject notification = new JSObject().put("id", ids.getInt(i));
jsArray.put(notification);
} catch (Exception ex) {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class LocalNotificationsHandler: NSObject, NotificationHandlerProtocol {
func makeNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {
let notificationRequest = notificationRequestLookup[request.identifier] ?? [:]
return [
"id": request.identifier,
"id": Int(request.identifier) ?? -1,
"title": request.content.title,
"sound": notificationRequest["sound"] ?? "",
"body": request.content.body,
Expand Down
13 changes: 10 additions & 3 deletions local-notifications/ios/Plugin/LocalNotificationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class LocalNotificationsPlugin: CAPPlugin {

let ret = ids.map({ (id) -> JSObject in
return [
"id": id
"id": Int(id) ?? -1
]
})
call.resolve([
Expand Down Expand Up @@ -137,7 +137,14 @@ public class LocalNotificationsPlugin: CAPPlugin {
return
}

let ids = notifications.map { $0["id"] as? String ?? "" }
let ids = notifications.map({ (value: JSObject) -> String in
if let idString = value["id"] as? String {
return idString
} else if let idNum = value["id"] as? NSNumber {
return idNum.stringValue
}
return ""
})

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ids)
call.resolve()
Expand Down Expand Up @@ -534,7 +541,7 @@ public class LocalNotificationsPlugin: CAPPlugin {

func makePendingNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {
return [
"id": request.identifier
"id": Int(request.identifier) ?? -1
]
}

Expand Down
2 changes: 1 addition & 1 deletion local-notifications/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export interface LocalNotificationDescriptor {
*
* @since 1.0.0
*/
id: string;
id: number;
}

export interface ScheduleOptions {
Expand Down
6 changes: 3 additions & 3 deletions local-notifications/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export class LocalNotificationsWeb

return {
notifications: options.notifications.map(notification => ({
id: notification.id.toString(),
id: notification.id,
})),
};
}

async getPending(): Promise<ScheduleResult> {
return {
notifications: this.pending.map(notification => ({
id: notification.id.toString(),
id: notification.id,
})),
};
}
Expand All @@ -55,7 +55,7 @@ export class LocalNotificationsWeb
async cancel(pending: ScheduleResult): Promise<void> {
this.pending = this.pending.filter(
notification =>
!pending.notifications.find(n => n.id === notification.id.toString()),
!pending.notifications.find(n => n.id === notification.id),
);
}

Expand Down