4
votes

I am trying to make a POST request to a Firebase Notifications API using Vapor 1.5 and Firebase Legacy Protocol, but I get failure response.

response is JSON(node: Node.Node.object(["multicast_id": Node.Node.number(5936281277445399934), "failure": Node.Node.number(0), "canonical_ids": Node.Node.number(0), "results": Node.Node.array([Node.Node.object(["message_id": Node.Node.string("0:1527074314969790%c7ade8b9f9fd7ecd")])]), "success": Node.Node.number(1)]))

EDIT Making the request through POSTMan fails with error "The request was missing an Authentication Key (FCM Token)."

class FirebaseRequester {
 let fcmLegacyServerKey = "AIzaSyDSuXXXXXXkCafTQay5_r8j3snvVos"

 func sendNotification(payLoad: JSON) throws -> Response {

    var response: Response?
    do {
        let responseFCM = try drop.client.post("https://fcm.googleapis.com/fcm/send", 
           headers: ["Content-Type":"application/json","Authorization": "key\(fcmLegacyServerKey)"], 
           query: [:], 
          body: payLoad.makeBody())

        response = responseFCM

    }catch let error {
        let message = error.localizedDescription
        logErr.prints(message: message)
        throw Abort.custom(status: .badRequest, message: message)
    }

    guard let rsp = response?.json else {


        let message = "no json received on line \(#line)"
        drop.log.error(message)
        logErr.prints(message: message)
        throw Abort.custom(status: .badRequest, message: message)
     }
  print("rsp in json format is \(rsp)")
      return response!
 }//end of sendNotification()
}//end of class FirebaseRequester




      //make another class here and initialize it with  FirebaseRequester
      //get data from Client App 
      // validate data 
      // finally, create the payLoad and call sendNotification(:)
     //request should look like 
{
  "aps": {
    "alert": "Breaking News!",
    "sound": "default",
    "link_url": "https://raywenderlich.com"
 }
}

     let fcmKeyToSendTo = "someDeviceTokenKeyReceivedFromClient_biHZNI-e9E53WEkCzrki"

            let data = try Node(node: ["alert": "alert", "sound": "sound", "link_url": "https://www.someWebsite.com"])

     var payLoadObj = try JSON(node: ["aps" : data])
     payLoadObj["to"] = try JSON(node: fcmKeyToSendTo)

            do {
                let _ = try firebaseRequester.sendNotification(payLoad: payLoadObj)
            }catch{
                logErr.prints(message: error.localizedDescription)
            }

            let message = "notification Sent"
            return try JSON(node:["success":message])

enter image description here enter image description here

1
Hey bibscy. Saw your comment from your previous post and got here. I haven't used vapor before, but should the Authorization value be formatted like "key\(fcmLegacyServerKey)"? shouldn't it be the plain server key value? If that doesn't work, maybe follow the format for when sending it via postman i.e. "key=(fcmLegacyServerKey)"?AL.
@AL. I had attached a screenshot from PostMan, and that request returns 401 too. That should return 200. Now, back to your question fcmLegacyServerKey: String is a variable, so using "key=(fcmLegacyServerKey) is legal, in essence it's like "key=123456AA"bibscy

1 Answers

1
votes
  1. In sendNotification(payload:) I had a typo, I missed = after key. It should have been "key=\(fcmLegacyServerKey)"
  2. In sendNotification(payload:), payLoad.makeBody should not be called, I should have just passed the JSON object payLoad as an argument to the .post request.
  3. The JSON object of the notification was clearly badly formatted from the outset. The message type I wanted to send was notification, but I was passing in a key named aps. I should have passed key notification as shown below.

.

class FirebaseRequester {

  let fcmLegacyServerKey = "AIzaSy....vVos"

  func sendNotification(payLoad: JSON) throws -> Response {

  var response: Response?
   do {
     let responseFCM = try drop.client.post("https://fcm.googleapis.com/fcm/send", 
       headers: ["Content-Type":"application/json","Authorization": "key=\(fcmLegacyServerKey)"], 
       query: [:], 
      body: payLoad

      response = responseFCM

  }catch let error {
     let message = error.localizedDescription
     logErr.prints(message: message)
     throw Abort.custom(status: .badRequest, message: message)
 }
   guard let rsp = response?.json else {

    let message = "no json received on line \(#line)"
    drop.log.error(message)
    logErr.prints(message: message)
    throw Abort.custom(status: .badRequest, message: message)
   }
    return response!
  }//end of sendNotification()
}//end of class FirebaseRequester



class TestRouteNow {

  let firebaseRequester: FirebaseRequester

  init(firebaseRequester: FirebaseRequester) {
     self.firebaseRequester = firebaseRequester
  }

  func addRoutes(drop: Droplet) {
     drop.post("test", "notif", handler: postNotification)
  }

   func postNotification(request: Request) throws -> ResponseRepresentable {

   let fcmDevice = "someDeviceTokenReceivedFromClientApp"
   let data = try Node(node: ["title": "title","body": "body", "sound": "default", "badge":"60"])

    var payLoadObj = try JSON(node: ["notification": data])
    payLoadObj["to"] = try JSON(node: fcmDevice)

      do {
        let _ = try firebaseRequester.sendNotification(payLoad: payLoadObj)
            }catch{
                logErr.prints(message: error.localizedDescription)
            }

            let message = "notification Sent"
            return try JSON(node:["success":message]) 
     } 
 }//end of class




    // request body
{
  "to" : "cQDtm_someDeviceTokenReceivedFromClient",
  "priority":"high",

 "notification": {
     "title":"Booking Rescheduled",
      "body": "Cancelled Booking 7830593, for Mon, 12 March",
      "sound":"default",
     "badge": "100"
  }
}