gpt4 book ai didi

swift - 如何使用 Firebase 上游消息发送可操作(丰富)的通知?

转载 作者:行者123 更新时间:2023-11-30 11:08:38 26 4
gpt4 key购买 nike

是否可以将 Firebase 上游消息作为可操作通知进行传递?这就是我想要实现的目标:我希望能够将可操作的上游 Firebase 消息从我的 iPad 发送到我的 iPhone。

这可能吗?

这是我的代码当前的样子。我能够成功发送远程通知并且 iPhone 收到它。我尝试了很多不同的方法来让它发送可操作的通知,但似乎没有任何效果。

let Category = UNNotificationCategory(identifier: "MESSAGE",
actions: [],
intentIdentifiers: [],
options: .customDismissAction)


let snoozeAction = UNNotificationAction(identifier: "EMERGENCY_MESSAGE_ACTION",
title: "Are you ok?",
options: UNNotificationActionOptions(rawValue: 0))



let center = UNUserNotificationCenter.current()
center.setNotificationCategories([Category])


let serverKey = "MY_SERVER_KEY"

let topic = ""
let url = URL(string: "https://fcm.googleapis.com/fcm/send")

let postParams = [
"to": "RECIEVING_DEVICE_TOKEN","mutable_content":true,


"notification": [
"body" : "\(emergencyMessage.text!)",
"title" : "EMERGENCY!",
"sound" : "alert.aiff", // or specify audio name to play
"message_id": "12345"
],


"data":[
"apns":[
"payload": [
"aps": [
"category": "MESSAGE"
]
]
]
]








]


as [String : Any]




let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")



do {
request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: JSONSerialization.WritingOptions())
print("My paramaters: \(postParams)")
} catch {
print("Caught an error: \(error)")
}



let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
if let realResponse = response as? HTTPURLResponse {
if realResponse.statusCode != 200 {
print("Not a 200 response")
}
}

if let postString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as String? {
print("POST: \(postString)")
}
}

.resume()
}

最佳答案

上游消息不会从一台设备直接发送到另一台设备。他们被发送from a device to an app server you control 。您的应用服务器必须实现XMPP protocol defined in the documentation 。在您的服务器上,您必须弄清楚如何向其他设备发送另一条消息。

关于swift - 如何使用 Firebase 上游消息发送可操作(丰富)的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52434773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com