首页 > 解决方案 > 如何使用 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()
}

标签: swiftfirebasefirebase-cloud-messaging

解决方案


上游消息不会从一台设备直接发送到另一台设备。它们从设备发送到您控制的应用服务器。您的应用服务器必须实现文档中定义的 XMPP 协议。在您的服务器上,您必须弄清楚如何向另一台设备发送另一条消息。


推荐阅读