-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.swift
372 lines (336 loc) · 13.9 KB
/
Plugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import Foundation
import Capacitor
import FirebaseMessaging
extension Date {
var year: Int {
return Calendar.current.component(.year, from: self)
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
var day: Int {
return Calendar.current.component(.day, from: self)
}
var hour: Int {
return Calendar.current.component(.hour, from: self)
}
var minute: Int {
return Calendar.current.component(.minute, from: self)
}
// change date to local timezone string
func toLocaleString(from date: Date? = nil) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale.current
return dateFormatter.string(from: date ?? Date())
}
}
/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitorjs.com/docs/plugins/ios
*/
@objc(NotificationExtension)
public class NotificationExtension: CAPPushNotificationsPlugin {
let sqlHandler = SQLiteHandler()
public override func load() {
super.load()
NotificationCenter.default.addObserver(
self,
selector: #selector(onNotification(_:)),
name: Notification.Name("SilentNotification"),
object: nil
)
}
// observer callback (SilentNotification)
@objc func onNotification(_ notification: Notification) {
handleNotification(notification: notification)
}
@objc func handleNotification(notification: Notification) -> Void {
let isLoggedIn: Bool = sqlHandler.isLoggedIn()
if (!isLoggedIn) {
return
}
// extract push data. if there is no data, return.
guard let notificationData: [String: Any] = notification.userInfo?["aps"] as? [String: Any] else {
return
}
// notify notification data to listner
notifyToListeners(identifier: "pushNotificationReceived", data: notificationData)
if checkMessageCondition(remoteMessageData: notificationData) {
UNUserNotificationCenter.current().getDeliveredNotifications { deliveredNotifications in
// if there is no identifier, finish this process
guard let identifier: String = self.getIdentifier(notificationData) else {
return
}
let content: UNMutableNotificationContent = self.getPushNotificationContent(notificationData, deliveredNotifications, identifier)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
self.showPushNotification(request: request)
}
}
}
@objc func getIdentifier(_ data: [String: Any]) -> String? {
if let identifier: String = data["code"] as? String {
return identifier
} else {
return nil
}
}
// get push notification content
@objc func getPushNotificationContent(_ data: [String: Any], _ deliveredData: [UNNotification], _ identifier: String) -> UNMutableNotificationContent {
let title: String = (data["title"] ?? "") as! String // title
let body: String = (data["body"] ?? "") as! String // body
// default badge value == 1
var badge: Int = (data["badge"] as? Int) ?? 1
// if there is duplicated notification, badge count is same as before
if let _: UNNotification = getDuplicatedNotification(data: deliveredData, identifier: identifier) {
badge = getDeliveredBadgeCount(data: deliveredData)
} else {
badge += getDeliveredBadgeCount(data: deliveredData)
}
let content = UNMutableNotificationContent()
content.title = title
// content.subtitle = subtitle
content.body = body
content.badge = badge as NSNumber
content.sound = UNNotificationSound.default
content.userInfo = data
return content
}
// get duplicated notification data from notifications which have been sent and not read(== delivered notifications).
@objc func getDuplicatedNotification(data: [UNNotification], identifier: String) -> UNNotification? {
let duplicatedNotifications: [UNNotification?] = data.filter { (noti: UNNotification) -> Bool in
let notificationRequest = noti.value(forKey: "request") as! UNNotificationRequest
if let notificationIdentifier: String = notificationRequest.value(forKey: "identifier") as? String {
return notificationIdentifier == identifier
} else {
return false
}
}
if duplicatedNotifications.count > 0 {
if let result: UNNotification = duplicatedNotifications[0] {
return result
}
}
return nil
}
// get count of delivered notifications
@objc func getDeliveredBadgeCount(data: [UNNotification]) -> Int {
return data.count
}
// show push notification to user
@objc func showPushNotification(request: UNNotificationRequest) -> Void {
DispatchQueue.main.async {
// send push only inactive state
let isActive = self.isApplicationActive()
if !isActive {
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
}
@objc func notifyToListeners(identifier: String, data: [String: Any]) -> Void {
DispatchQueue.main.async {
let isActive: Bool = self.isApplicationActive()
if let isShown: String = data["isShown"] as? String {
// notify to listeners if application is active or "isShown" is false
if isActive || isShown == "false" {
self.notifyListeners(identifier, data: data)
}
}
}
}
/**
* get is application active ( return true when application is in foreground )
*/
@objc func isApplicationActive() -> Bool {
switch UIApplication.shared.applicationState {
case .active:
return true
default:
return false
}
}
@objc func getToken(_ call: CAPPluginCall) {
Messaging.messaging().token { token, error in
if let error = error {
call.error("Failed to get instance FirebaseID", error)
} else if let token = token {
call.success([
"value": token
]);
}
}
}
@objc func addTimeFilter(_ call: CAPPluginCall) -> Void {
// add timeFilter only when PluginCall has "startFrom" and "endAt"
if let startFrom = call.getString("startFrom"), let endAt = call.getString("endAt") {
handleResult(result: sqlHandler.insertTimeFilter(startFrom: startFrom, endAt: endAt), call)
}
}
@objc func removeTimeFilter(_ call: CAPPluginCall) -> Void {
handleResult(result: sqlHandler.removeTimeFilter(), call)
}
@objc func addFilters(_ call: CAPPluginCall) -> Void {
guard let filters: [String] = call.getArray("filters", String.self) else {
call.reject("filters undefined")
return;
}
for filter in filters {
let result: Bool = isSucceedResult(result: sqlHandler.insertFilter(key: filter), call)
if !result {
return;
}
}
call.success()
}
@objc func removeFilters(_ call: CAPPluginCall) -> Void {
guard let filters: [String] = call.getArray("filters", String.self) else {
call.reject("filters undefined")
return;
}
for filter in filters {
let result: Bool = isSucceedResult(result: sqlHandler.removeFilter(key: filter), call)
if !result {
return;
}
}
call.success()
}
@objc func handleResult(result: [String: Any], _ call: CAPPluginCall) {
if result["success"] as! Bool {
call.success()
} else {
if let reason: String = result["reason"] as? String {
call.reject(reason)
} else {
call.reject("Something when wrong")
}
}
}
// processing after database DML
@objc func isSucceedResult(result: [String: Any], _ call: CAPPluginCall) -> Bool {
let boolResult: Bool = result["success"] as! Bool
if !boolResult {
if let reason: String = result["reason"] as? String {
call.reject(reason)
} else {
call.reject("Database quering Error: Something went wrong")
}
}
return boolResult
}
/**
* extract default date from HH:mm string
*/
@objc func extractDefaultDate(data: String) -> Date? {
let splittedData = data.components(separatedBy: ":")
if splittedData.count != 2 {
return nil
}
let calendar = Calendar.current
let dateComponent = DateComponents(
year: 2021,
hour: Int(splittedData[0]),
minute: Int(splittedData[1])
)
return calendar.date(from: dateComponent)!
}
/**
* if currentDate is between startFrom and endAt, return false
*/
@objc func compareDate(_ startFrom: String, _ endAt: String) -> Bool {
let date = Date()
let calendar = Calendar.current
guard let startFromDate: Date = extractDefaultDate(data: startFrom),
var currentDate: Date = extractDefaultDate(data: "\(date.hour):\(date.minute)"),
var endAtDate: Date = extractDefaultDate(data: endAt) else {
return false;
}
if startFromDate > endAtDate {
endAtDate = calendar.date(byAdding: .day, value: 1, to: endAtDate)!
// if current is lower than start, add 1 day to currentDate
if (startFromDate > currentDate) {
currentDate = calendar.date(byAdding: .day, value: 1, to: currentDate)!
}
}
return !((startFromDate < currentDate) && (currentDate < endAtDate))
}
@objc func isValidTime() -> Bool {
sqlHandler.createFilterTable()
let timeFilters = sqlHandler.getTimeFilter()
func getFilteredData(filterString: String) -> Array<[String: Any]> {
return timeFilters.filter { (filter: [String: Any]) -> Bool in
if let key: String = filter["key"] as? String {
return key == filterString
}
return false
}
}
let startTimeFilter: Array<[String: Any]> = getFilteredData(filterString: "filter_start_from")
let endTimeFilter: Array<[String: Any]> = getFilteredData(filterString: "filter_end_at")
let isTimeFilterOnObject: Array<[String: Any]> = getFilteredData(filterString: "is_time_filter_on")
// if there is no time filter or filterOn == false -> return true (can receive push-noti anytime)
if isTimeFilterOnObject.count > 0 {
if let timeFilterOn: String = isTimeFilterOnObject[0]["value"] as? String {
if timeFilterOn == "false" {
return true
}
}
}
if startTimeFilter.count > 0 && endTimeFilter.count > 0 {
guard let startFrom: String = startTimeFilter[0]["value"] as? String,
let endAt: String = endTimeFilter[0]["value"] as? String else {
return false
}
return compareDate(startFrom, endAt)
} else {
return true
}
}
@objc func isValidCondition(_ remoteMessageData: [String: Any]) -> Bool {
guard let filterString: String = remoteMessageData["filter"] as? String else {
// return true if there is no filter
return true
}
let filterList: Array<String> = processFilterString(filterString: filterString)
sqlHandler.createFilterTable()
let savedFilters: Array<[String: Any]> = sqlHandler.getFilters()
let matchedFilters: Array<[String: Any]> = savedFilters.filter { (filter: [String: Any]) -> Bool in
if let key = filter["key"] as? String {
return filterList.contains(key)
} else {
return false
}
}
for matchedFilter in matchedFilters {
if let value: String = matchedFilter["value"] as? String {
return value == "true"
}
}
return true
}
@objc func checkMessageCondition(remoteMessageData: [String: Any]) -> Bool {
return shouldMessageShown(remoteMessageData) && isValidTime() && isValidCondition(remoteMessageData)
}
@objc func processFilterString(filterString: String) -> Array<String> {
var mutableFilterString: String = filterString
if (mutableFilterString.hasSuffix(",")) {
let endIndex: String.Index = mutableFilterString.index(mutableFilterString.startIndex, offsetBy: mutableFilterString.count - 1)
mutableFilterString = String(mutableFilterString[...endIndex])
}
return mutableFilterString.split(separator: ",").map({ (value: Substring) -> String in
return String(value.trimmingCharacters(in: .whitespacesAndNewlines))
})
}
@objc func shouldMessageShown(_ remoteMessageData: [String: Any]) -> Bool {
if let messageShown: String = remoteMessageData["isShown"] as? String {
return messageShown == "true"
} else {
return true
}
}
@objc func getFilters(_ call: CAPPluginCall) -> Void {
let allFilters: Array<[String: Any]> = sqlHandler.getAllFilters()
call.success(["value": allFilters])
}
}