Skip to content

Commit e2e0b94

Browse files
committed
no message
1 parent b241210 commit e2e0b94

File tree

4 files changed

+85
-42
lines changed

4 files changed

+85
-42
lines changed

Sources/Pam/PAMHelper.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

Sources/Pam/PAMUtils.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by narongrit kanhanoi on 29/3/2564 BE.
6+
//
7+
8+
import Foundation
9+
import UserNotifications
10+
11+
public class PAMUtils {
12+
static func prettify(dict: [String: Any]) -> String {
13+
let jsonData = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
14+
return String(data: jsonData, encoding: .utf8) ?? ""
15+
}
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+
}
23+
24+
public static func isPamNotification(_ notification: UNNotification) -> Bool{
25+
if notification.request.content.userInfo["pam"] != nil {
26+
return true
27+
}
28+
return false
29+
}
30+
31+
public static func convertUNNotificationToPam(_ notification: UNNotification) -> PamPushMessage? {
32+
if let data = notification.request.content.userInfo["pam"] as? NSDictionary {
33+
let url = data.object(forKey: "url") as? String
34+
let popupType = data.object(forKey: "popupType") as? String
35+
let pixel = data.object(forKey: "pixel") as? String
36+
let flex = data.object(forKey: "flex") as? String
37+
38+
let aps = notification.request.content.userInfo["aps"] as? [String: Any]
39+
let alert = aps?["alert"] as? [String: String]
40+
let title = alert?["title"]
41+
let body = alert?["body"]
42+
43+
var payload:[String: Any] = [:]
44+
for (value, key) in data {
45+
payload[key as! String] = value
46+
}
47+
48+
return PamPushMessage(
49+
deliverID: nil,
50+
pixel: pixel,
51+
title: title,
52+
description: body,
53+
thumbnailUrl: nil,
54+
flex: flex,
55+
url: url,
56+
popupType: popupType,
57+
isRead: false,
58+
date: Date(),
59+
data: payload,
60+
pam: nil)
61+
}
62+
63+
return nil
64+
}
65+
}
66+

Sources/Pam/Pam.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public class Pam: NSObject {
345345
if isEnableLog {
346346
print("\n\n🦄 PAM : POST Event = 🍀\(event)🍀")
347347
print("🦄 PAM : Payload")
348-
print("🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉\n",PAMHelper.prettify(dict: body), "\n🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉\n\n")
348+
print("🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉\n",PAMUtils.prettify(dict: body), "\n🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉\n\n")
349349
}
350350

351351
HttpClient.post(url: url, queryString: nil, headers: nil, json: body) { res in

Sources/Pam/noti/models/PamPushMessage.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public struct PamPushMessage {
1818
public let popupType:String?
1919
public let isRead: Bool
2020
public let date: Date?
21-
public let data: NSDictionary?
22-
public let pam: NSDictionary?
21+
public let data: [String: Any]?
22+
public let pam: [String: Any]?
2323

2424
public func read(){
2525
NotificationAPI.read(message: self)
@@ -30,23 +30,22 @@ public struct PamPushMessage {
3030

3131
var arr: [PamPushMessage]?
3232

33-
if let json = try? JSONSerialization.jsonObject(with: data) as? NSDictionary{
33+
if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
3434

35-
if let items = json.object(forKey:"items") as? [NSDictionary] {
35+
if let items = json["items"] as? [[String: Any]] {
3636
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
37+
let deliverID = item["deliver_id"] as? String
38+
let pixel = item["pixel"] as? String
39+
let title = item["title"] as? String
40+
let description = item["description"] as? String
41+
let thumbnailUrl = item["thumbnail_url"] as? String
42+
let flex = item["flex"] as? String
43+
let url = item["url"] as? String
44+
let data = item["json_data"] as? [String: Any]
45+
let pam = item["pam"] as? [String: Any]
46+
let popupType = item["popupType"] as? String
47+
let date = item["created_date"] as? String
48+
let isRead = item["is_open"] as? Bool
5049

5150
return PamPushMessage(
5251
deliverID: deliverID,
@@ -58,11 +57,12 @@ public struct PamPushMessage {
5857
url: url,
5958
popupType: popupType,
6059
isRead: isRead ?? true,
61-
date: PAMHelper.dateFrom(string: date),
60+
date: PAMUtils.dateFrom(string: date),
6261
data: data,
6362
pam: pam)
6463
}
6564
}
65+
6666
}
6767

6868
return arr ?? []

0 commit comments

Comments
 (0)