Skip to content

Commit

Permalink
fix(local-notifications): extra not being returned on notification ev…
Browse files Browse the repository at this point in the history
…ents (#340)
  • Loading branch information
jcesarmobile committed Apr 12, 2021
1 parent ab8eecb commit 5b03a7f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
44 changes: 38 additions & 6 deletions local-notifications/ios/Plugin/LocalNotificationsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,46 @@ public class LocalNotificationsHandler: NSObject, NotificationHandlerProtocol {
*/
func makeNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {
let notificationRequest = notificationRequestLookup[request.identifier] ?? [:]
return [
var notification = makePendingNotificationRequestJSObject(request)
notification["sound"] = notificationRequest["sound"] ?? ""
notification["actionTypeId"] = request.content.categoryIdentifier
notification["attachments"] = notificationRequest["attachments"] ?? []
return notification

}

func makePendingNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {
var notification: JSObject = [
"id": Int(request.identifier) ?? -1,
"title": request.content.title,
"sound": notificationRequest["sound"] ?? "",
"body": request.content.body,
"extra": request.content.userInfo as? JSObject ?? [:],
"actionTypeId": request.content.categoryIdentifier,
"attachments": notificationRequest["attachments"] ?? []
"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

}
}
37 changes: 1 addition & 36 deletions local-notifications/ios/Plugin/LocalNotificationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public class LocalNotificationsPlugin: CAPPlugin {
CAPLog.print(notifications)

let ret = notifications.compactMap({ [weak self] (notification) -> JSObject? in
return self?.makePendingNotificationRequestJSObject(notification)
return self?.notificationDelegationHandler.makePendingNotificationRequestJSObject(notification)
})

call.resolve([
Expand Down Expand Up @@ -546,41 +546,6 @@ public class LocalNotificationsPlugin: CAPPlugin {
return opts
}

func makePendingNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {
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) {
call.unimplemented()
}
Expand Down

0 comments on commit 5b03a7f

Please sign in to comment.