How to send a POST request to Firebase Cloud Messaging API in Vapor

bibscy

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

bibscy
  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"
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Post request with ajax for Firebase Cloud Messaging

Firebase Cloud Messaging Send HTTP-Post in Java

How to send device to device messages using Firebase Cloud Messaging?

How to send the Group Notifications using Firebase Cloud messaging?

Firebase Cloud Messaging: how to send data message to all users?

how to send silent notification / background notification using Firebase Cloud Messaging?

Firebase Cloud Messaging for web: how to send to multiple tokens?

How to send a Firebase Cloud Messaging Notification with special characters like ä, ö, ü?

FCM (Firebase Cloud Messaging) how to send to all Phones?

How to send notification from app using Firebase Cloud Messaging?

How to write Firebase Cloud Messaging function to send push notification

How to send POST request by Spring cloud Feign

Firebase Cloud Messaging Statistics API

How to send post request to Firebase function?

Send a POST request via Axios to a Firebase Cloud Function

Send SMS using POST request from Firebase Cloud Function

Not able to send an iOS CriticalAlert to Firebase Cloud Messaging

FCM (Firebase Cloud Messaging) Send to multiple devices

Firebase Cloud Messaging - Send message to all users

firebase cloud messaging Request contains an invalid argument

Firebase Cloud Messaging AJAX POST in JavaScript

POST message to Firebase Cloud Messaging via NodeJS

How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

How to send POST request?

How to send Firebase Cloud Messaging notification based on user property, user audiences

How we can send image and icon by using Firebase cloud messaging(fcm) for push notification in android

How to use fcm(firebase cloud messaging) with scala play framework to send notification?

Firebase Cloud Messaging request DeviceID without connecting to Firebase

How do you successfully send post request to firebase function