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

时间:2018-09-20 23:51:23

标签: swift firebase firebase-cloud-messaging

是否可以将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()
}

1 个答案:

答案 0 :(得分:0)

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