如何保存 pushNotification 附带的两个项目
How to save two items which comes with pushNotification
1)收到通知时,我需要保存来自 AnyHashable("data") "amount" 和 "inBalance"?
的数据
2) 从服务器发出两次推送,第一次推送 "message",第二次推送 "nil message body"。当出现 nil 消息时,我需要静音吗?
我怎样才能保存它?
我正在使用 xCode 10.2.1
我的 pushNotification.swift 代码如下:
enum PillikanRemoteNotification:Int {
case empty
case notification = 2
case news = 3
}
class PushManagerNotification {
func convert(with value: [AnyHashable: Any], and state: PushNotificationAction) {
guard let json = value as? [String: AnyObject], !json.isEmpty else { return }
guard let jsonString = value["data"] as? String, !jsonString.isEmpty else { return }
guard let jsonData = jsonString.data(using: .utf8) else { return }
guard let rawDictionary = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) else { return }
guard let dictionary = rawDictionary as? [String: AnyObject] else { return }
guard let actionType = dictionary["action"] as? String, let action = Int(actionType) else { return }
guard let notificationType = PillikanRemoteNotification(rawValue: action) else { return }
guard let messageBody = dictionary["msg"] as? [String: AnyObject] else { return }
if state == .show {
showBadges(dictionary: messageBody)
return
}
switch notificationType {
case .notification:
showNotification(dictionary: messageBody)
break
default:
break;
}
}
private func showBadges(dictionary: [String: AnyObject]) {
guard let badgeMessage = dictionary["badges"] as? [String: AnyObject] else { return }
guard let badges = Badges(JSON: badgeMessage) else { return }
UIApplication.shared.notificationBadgesForNotification = badges.notifications
UIApplication.shared.notificationBadgesForNews = badges.news
UIApplication.shared.applicationIconBadgeNumber = badges.news + badges.notifications
NotificationCenter.default.post(name: .badges, object: badges)
}
private func showNotification(dictionary: [String:AnyObject]) {
if let message = NotificationEntity(JSON: dictionary) {
NotificationCenter.default.post(name: .notification, object: message);
}
}
extension Notification.Name {
static let empty = Notification.Name("empty");
static let notification = Notification.Name("notification");
static let news = Notification.Name("news");
static let badges = Notification.Name("badges")
}
在 didReceive
函数中使用这个:
let amount = userInfo["data"]["amount"]
let inBalance = userInfo["data"]["inBalance"]
我不确定这是否有效我没有尝试过.. 试试这个通知属性的功能:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
let message = userInfo["message"] as! String
if message.isEmpty == true {
completionHandler([.alert, .badge])
}else {
completionHandler([.alert, .badge, .sound])
}
}
对于静默通知有两个标准:
1.) 负载的 aps 字典必须包含值为 1 的 content-available 键。
2.) 负载的 aps 字典不得包含警报、声音或徽章键。
例子
{
"aps":{
"content-available":1
},
"cusomtkey1" : "bar",
"cusomtkey2" : 42
}
在这里你可以看到,我是如何从 AnyHashable 中获取令牌,然后从中获取特定值的。
试试这个代码。它会帮助你。
if(response.result.isSuccess){
let data = response.result.value
print(data!)
if (data?.contains("access_token"))!{
var dictonary:NSDictionary?
if let data = data!.data(using: String.Encoding.utf8) {
do {
dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] as NSDictionary?
UserDefaults.standard.set(dictonary!["access_token"]!, forKey: "token")
}catch let error as NSError {
print(error)
}
}
}
}
1)收到通知时,我需要保存来自 AnyHashable("data") "amount" 和 "inBalance"?
的数据2) 从服务器发出两次推送,第一次推送 "message",第二次推送 "nil message body"。当出现 nil 消息时,我需要静音吗? 我怎样才能保存它? 我正在使用 xCode 10.2.1
我的 pushNotification.swift 代码如下:
enum PillikanRemoteNotification:Int {
case empty
case notification = 2
case news = 3
}
class PushManagerNotification {
func convert(with value: [AnyHashable: Any], and state: PushNotificationAction) {
guard let json = value as? [String: AnyObject], !json.isEmpty else { return }
guard let jsonString = value["data"] as? String, !jsonString.isEmpty else { return }
guard let jsonData = jsonString.data(using: .utf8) else { return }
guard let rawDictionary = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) else { return }
guard let dictionary = rawDictionary as? [String: AnyObject] else { return }
guard let actionType = dictionary["action"] as? String, let action = Int(actionType) else { return }
guard let notificationType = PillikanRemoteNotification(rawValue: action) else { return }
guard let messageBody = dictionary["msg"] as? [String: AnyObject] else { return }
if state == .show {
showBadges(dictionary: messageBody)
return
}
switch notificationType {
case .notification:
showNotification(dictionary: messageBody)
break
default:
break;
}
}
private func showBadges(dictionary: [String: AnyObject]) {
guard let badgeMessage = dictionary["badges"] as? [String: AnyObject] else { return }
guard let badges = Badges(JSON: badgeMessage) else { return }
UIApplication.shared.notificationBadgesForNotification = badges.notifications
UIApplication.shared.notificationBadgesForNews = badges.news
UIApplication.shared.applicationIconBadgeNumber = badges.news + badges.notifications
NotificationCenter.default.post(name: .badges, object: badges)
}
private func showNotification(dictionary: [String:AnyObject]) {
if let message = NotificationEntity(JSON: dictionary) {
NotificationCenter.default.post(name: .notification, object: message);
}
}
extension Notification.Name {
static let empty = Notification.Name("empty");
static let notification = Notification.Name("notification");
static let news = Notification.Name("news");
static let badges = Notification.Name("badges")
}
在 didReceive
函数中使用这个:
let amount = userInfo["data"]["amount"]
let inBalance = userInfo["data"]["inBalance"]
我不确定这是否有效我没有尝试过.. 试试这个通知属性的功能:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
let message = userInfo["message"] as! String
if message.isEmpty == true {
completionHandler([.alert, .badge])
}else {
completionHandler([.alert, .badge, .sound])
}
}
对于静默通知有两个标准:
1.) 负载的 aps 字典必须包含值为 1 的 content-available 键。
2.) 负载的 aps 字典不得包含警报、声音或徽章键。
例子
{ "aps":{ "content-available":1 }, "cusomtkey1" : "bar", "cusomtkey2" : 42 }
在这里你可以看到,我是如何从 AnyHashable 中获取令牌,然后从中获取特定值的。
试试这个代码。它会帮助你。
if(response.result.isSuccess){
let data = response.result.value
print(data!)
if (data?.contains("access_token"))!{
var dictonary:NSDictionary?
if let data = data!.data(using: String.Encoding.utf8) {
do {
dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] as NSDictionary?
UserDefaults.standard.set(dictonary!["access_token"]!, forKey: "token")
}catch let error as NSError {
print(error)
}
}
}
}