Skip to content

Commit 56d764a

Browse files
committed
Merge branch 'release/1.3.0'
2 parents 81f5d83 + 5e96be0 commit 56d764a

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

Sources/Pam/PAMHelper.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ class PAMHelper {
1313
let jsonData = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
1414
return String(data: jsonData, encoding: .utf8) ?? ""
1515
}
16+
static func dateFrom(string: String?) -> Date?{
17+
guard let date = string else {return nil}
18+
let dateFormatter = DateFormatter()
19+
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
20+
let dateObj = dateFormatter.date(from:date)
21+
return dateObj
22+
}
1623
}

Sources/Pam/Pam+Static.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,22 @@ extension Pam {
183183
HttpClient.getReturnData(url: url, queryString: nil, headers: nil, onSuccess: nil)
184184
}
185185

186+
public static func loadPushNotifications(email: String, onLoad: @escaping ([PamPushMessage])->Void){
187+
NotificationAPI.loadPushNotifications(email: email){ list in
188+
onLoad(list)
189+
}
190+
}
191+
192+
public static func loadPushNotifications(mobile: String, onLoad: @escaping ([PamPushMessage])->Void){
193+
NotificationAPI.loadPushNotifications(mobile: mobile){ list in
194+
onLoad(list)
195+
}
196+
}
197+
198+
public static func loadPushNotifications(customerID: String, onLoad: @escaping ([PamPushMessage])->Void){
199+
NotificationAPI.loadPushNotifications(customerID: customerID){ list in
200+
onLoad(list)
201+
}
202+
}
203+
186204
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// NotificationAPI.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 1/3/2565 BE.
6+
//
7+
8+
import Foundation
9+
10+
class NotificationAPI {
11+
12+
typealias OnLoadNotifications = ([PamPushMessage])->Void
13+
14+
static func read(message:PamPushMessage?){
15+
guard let pixel = message?.pixel else { return }
16+
17+
HttpClient.getReturnData(url: pixel, queryString: nil, headers: nil,onSuccess: nil)
18+
}
19+
20+
static func loadPushNotifications(mobile: String, onLoad: OnLoadNotifications? = nil ){
21+
let db = Pam.getDatabaseAlias()
22+
let contactID = Pam.getContactID() ?? "-"
23+
let pamServerURL = Pam.shared.config?.pamServer ?? ""
24+
25+
let endpoint = "\(pamServerURL)/api/app-notifications/?_database=\(db)&_contact_id=\(contactID)&sms=\(mobile)"
26+
27+
HttpClient.getReturnData(url: endpoint, queryString: nil, headers: nil){ data in
28+
guard let data = data else {
29+
onLoad?([])
30+
return
31+
}
32+
33+
let res = String(data:data, encoding: .utf8)
34+
let messages = PamPushMessage.parse(json: res)
35+
onLoad?(messages)
36+
}
37+
}
38+
39+
static func loadPushNotifications(email: String, onLoad: OnLoadNotifications? = nil){
40+
let db = Pam.getDatabaseAlias()
41+
let contactID = Pam.getContactID() ?? "-"
42+
let pamServerURL = Pam.shared.config?.pamServer ?? ""
43+
44+
let endpoint = "\(pamServerURL)/api/app-notifications/?_database=\(db)&_contact_id=\(contactID)&email=\(email)"
45+
46+
HttpClient.getReturnData(url: endpoint, queryString: nil, headers: nil){ data in
47+
guard let data = data else {
48+
onLoad?([])
49+
return
50+
}
51+
52+
let res = String(data:data, encoding: .utf8)
53+
let messages = PamPushMessage.parse(json: res)
54+
onLoad?(messages)
55+
}
56+
}
57+
58+
static func loadPushNotifications(customerID: String, onLoad: OnLoadNotifications? = nil){
59+
let db = Pam.getDatabaseAlias()
60+
let contactID = Pam.getContactID() ?? "-"
61+
let pamServerURL = Pam.shared.config?.pamServer ?? ""
62+
63+
let endpoint = "\(pamServerURL)/api/app-notifications/?_database=\(db)&_contact_id=\(contactID)&customer=\(customerID)"
64+
65+
HttpClient.getReturnData(url: endpoint, queryString: nil, headers: nil){ data in
66+
guard let data = data else {
67+
onLoad?([])
68+
return
69+
}
70+
71+
let res = String(data:data, encoding: .utf8)
72+
let messages = PamPushMessage.parse(json: res)
73+
onLoad?(messages)
74+
}
75+
}
76+
77+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 15/3/2565 BE.
6+
//
7+
8+
import Foundation
9+
10+
public struct PamPushMessage {
11+
public let deliverID:String?
12+
public let pixel:String?
13+
public let title:String?
14+
public let description:String?
15+
public let thumbnailUrl:String?
16+
public let flex:String?
17+
public let url:String?
18+
public let popupType:String?
19+
public let isRead: Bool
20+
public let date: Date?
21+
public let data: NSDictionary?
22+
public let pam: NSDictionary?
23+
24+
public func read(){
25+
NotificationAPI.read(message: self)
26+
}
27+
28+
static func parse(json: String?)-> [PamPushMessage] {
29+
guard let data = json?.data(using: .utf8) else{ return [] }
30+
31+
var arr: [PamPushMessage]?
32+
33+
if let json = try? JSONSerialization.jsonObject(with: data) as? NSDictionary{
34+
35+
if let items = json.object(forKey:"items") as? [NSDictionary] {
36+
arr = items.map { item in
37+
38+
let deliverID = item.object(forKey:"deliver_id") as? String
39+
let pixel = item.object(forKey:"pixel") as? String
40+
let title = item.object(forKey:"title") as? String
41+
let description = item.object(forKey:"description") as? String
42+
let thumbnailUrl = item.object(forKey:"thumbnail_url") as? String
43+
let flex = item.object(forKey:"flex") as? String
44+
let url = item.object(forKey:"url") as? String
45+
let data = item.object(forKey:"json_data") as? NSDictionary
46+
let pam = item.object(forKey:"pam") as? NSDictionary
47+
let popupType = item.object(forKey:"popupType") as? String
48+
let date = item.object(forKey:"created_date") as? String
49+
let isRead = item.object(forKey:"is_open") as? Bool
50+
51+
return PamPushMessage(
52+
deliverID: deliverID,
53+
pixel: pixel,
54+
title: title,
55+
description: description,
56+
thumbnailUrl: thumbnailUrl,
57+
flex: flex,
58+
url: url,
59+
popupType: popupType,
60+
isRead: isRead ?? true,
61+
date: PAMHelper.dateFrom(string: date),
62+
data: data,
63+
pam: pam)
64+
}
65+
}
66+
}
67+
68+
return arr ?? []
69+
}
70+
}

0 commit comments

Comments
 (0)