Skip to content

Commit 8abb31e

Browse files
fix(notifications): crash on ios when scheduling a notification (#2905)
* Converts NotificationSchedule enum to a dictionary to fix crash while scheduleing notifications. * change file --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 0354046 commit 8abb31e

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"notification": patch
3+
"notification-js": patch
4+
---
5+
6+
Fix notification scheduling on iOS.

plugins/notification/ios/Sources/Notification.swift

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ func makeNotificationContent(_ notification: Notification) throws -> UNNotificat
3838
arguments: nil)
3939
}
4040

41-
content.userInfo = [
42-
"__EXTRA__": notification.extra as Any,
43-
"__SCHEDULE__": notification.schedule as Any,
44-
]
41+
var userInfo: [String: Any] = [:]
42+
43+
if let extra = notification.extra {
44+
userInfo["__EXTRA__"] = extra
45+
}
46+
47+
if let schedule = notification.schedule {
48+
userInfo["__SCHEDULE__"] = scheduleToDictionary(schedule)
49+
}
50+
51+
content.userInfo = userInfo
4552

4653
if let actionTypeId = notification.actionTypeId {
4754
content.categoryIdentifier = actionTypeId
@@ -66,6 +73,56 @@ func makeNotificationContent(_ notification: Notification) throws -> UNNotificat
6673
return content
6774
}
6875

76+
func scheduleToDictionary(_ schedule: NotificationSchedule) -> [String: Any] {
77+
switch schedule {
78+
case .at(let date, let repeating):
79+
return [
80+
"type": "at",
81+
"date": date,
82+
"repeating": repeating
83+
]
84+
case .interval(let interval):
85+
return [
86+
"type": "interval",
87+
"interval": scheduleIntervalToDictionary(interval)
88+
]
89+
case .every(let interval, let count):
90+
return [
91+
"type": "every",
92+
"interval": interval.rawValue,
93+
"count": count
94+
]
95+
}
96+
}
97+
98+
func scheduleIntervalToDictionary(_ interval: ScheduleInterval) -> [String: Any] {
99+
var dict: [String: Any] = [:]
100+
101+
if let year = interval.year {
102+
dict["year"] = year
103+
}
104+
if let month = interval.month {
105+
dict["month"] = month
106+
}
107+
if let day = interval.day {
108+
dict["day"] = day
109+
}
110+
if let weekday = interval.weekday {
111+
dict["weekday"] = weekday
112+
}
113+
if let hour = interval.hour {
114+
dict["hour"] = hour
115+
}
116+
if let minute = interval.minute {
117+
dict["minute"] = minute
118+
}
119+
if let second = interval.second {
120+
dict["second"] = second
121+
}
122+
123+
return dict
124+
}
125+
69126
func makeAttachments(_ attachments: [NotificationAttachment]) throws -> [UNNotificationAttachment] {
70127
var createdAttachments = [UNNotificationAttachment]()
71128

0 commit comments

Comments
 (0)