Ios 如何保存pushNotification附带的两个项目
1) 当通知到来时,我需要保存来自任何哈希(“数据”)“金额”和“不平衡”的数据 2) 从服务器来了两次推送,第一次推送带有“message”,第二次推送带有“nil message body”。当nil message到来时,我需要让它静音吗? 我怎样才能拯救它? 我使用的是xCode 10.2.1 我的pushNotification.swift代码如下所示:Ios 如何保存pushNotification附带的两个项目,ios,swift,xcode,rx-swift,Ios,Swift,Xcode,Rx Swift,1) 当通知到来时,我需要保存来自任何哈希(“数据”)“金额”和“不平衡”的数据 2) 从服务器来了两次推送,第一次推送带有“message”,第二次推送带有“nil message body”。当nil message到来时,我需要让它静音吗? 我怎样才能拯救它? 我使用的是xCode 10.2.1 我的pushNotification.swift代码如下所示: enum PillikanRemoteNotification:Int { case empty case noti
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])
}
}
在
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的内容可用键
2.)有效负载的aps字典不得包含警报、声音或徽章键
范例
{
“aps”:{
“可用内容”:1
},
“cusomtkey1”:“条形图”,
“cusomtkey2”:42
}对于静默通知,有两个标准:
1.)有效负载的aps字典必须包含值为1的内容可用键
2.)有效负载的aps字典不得包含警报、声音或徽章键
范例
{
“aps”:{
“可用内容”:1
},
“cusomtkey1”:“条形图”,
“cusomtkey2”:42
}
在这里你们可以看到,我是如何从任何hashable中获取令牌,然后从中获取特定值的
试试这个代码。它会帮你的
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)
}
}
}
}
在这里你们可以看到,我是如何从任何hashable中获取令牌,然后从中获取特定值的
试试这个代码。它会帮你的
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)
}
}
}
}
我一次收到两个推送通知。第二次推送带有nil“message etc”。我需要在没有声音的情况下转换。对不起,“amount”和“inBalance”在我的代码中不起作用。让amount=userInfo[“amount”]?我一次收到两个推送通知。第二次推送带有nil“message etc.”我需要在没有声音的情况下转换。对不起,“金额”和“不平衡”在我的代码中不起作用如何
let amount=userInfo[“金额”]
?