swift - 如何使用 Firebase 上游消息发送可操作(丰富)通知?
问题描述
是否可以将 Firebase 上游消息作为可操作通知传递?这就是我想要完成的:我希望能够从我的 iPad 向我的 iPhone 发送一条可操作的上游 firebase 消息。
这可能吗?
这是我的代码目前的样子。我能够成功发送远程通知并且 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()
}
解决方案
上游消息不会从一台设备直接发送到另一台设备。它们从设备发送到您控制的应用服务器。您的应用服务器必须实现文档中定义的 XMPP 协议。在您的服务器上,您必须弄清楚如何向另一台设备发送另一条消息。
推荐阅读
- php - 显示 count_all_results 记录以查看
- vue.js - Vuex:如何将变量传递给 getter?
- java - 2048 game Java:如何根据用户输入打印游戏板?
- javascript - 如何使用 chart.js 3.0.0 在 React 上下文中访问 Chart.helpers.color?
- css - 使用 CSS 旋转和裁剪 svg 背景
- python - BeautifulSoup 昨天没有获取所有 HTML 元素,并且没有更改任何代码
- node.js - tf.image.cropAndResize 抛出“方法必须是双线性的或最近的,但未定义”错误
- sql - 在 SQL 中选择更改值的开始和结束日期
- reactjs - React Router v6 Modal 在单独的路由中
- php - 将接口绑定到实现的问题