0

Is there any way to send push notification from a device to device directly using https://fcm.googleapis.com/fcm/send.

My code:

func sendPushNotification(toUser: String, message: String) {
    let urlString = "https://fcm.googleapis.com/fcm/send"
    let url = NSURL(string: urlString)!
    let paramString  = "to="

    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "POST"
    request.httpBody = paramString.data(using: String.Encoding.utf8)!
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

    let task =  URLSession.shared.dataTask(with: request as URLRequest)  { (data, response, error) in
        do {
            if let jsonData = data {
                if let jsonDataDict  = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
                    NSLog("Received data:\n\(jsonDataDict))")
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
        }
    }
    task.resume()
}
aofdev
  • 1,772
  • 1
  • 15
  • 29
Mohanraj
  • 587
  • 4
  • 21

2 Answers2

1
let paramString  = ["to" : FCM_ID/UDID, "notification" : ["title" : NOTIFICATION_TITLE, "body" : NOTIFICATION_BODY], "data" : ["user" : USER_ID, "image" : IMAGE_URL, "extrainfo" : ANY_STRING]]

just replace your line with this one and push notification should deliver.

Read more about it here... https://firebase.google.com/docs/cloud-messaging/server

Prateek Varshney
  • 1,114
  • 2
  • 12
  • 29
  • can u please help me with how to create NOTIFICATION_BODY – Mohanraj Sep 19 '17 at 12:55
  • Its just a simple string. It is the message that shows up on the notification banner below the title. Everything in caps is just a string that you need to replace. – Prateek Varshney Sep 19 '17 at 12:57
  • i want to send some contents inside the body like image url, user id – Mohanraj Sep 19 '17 at 12:59
  • Then you need to add data key value also. Updated my answer, please check. – Prateek Varshney Sep 19 '17 at 13:03
  • how and where to include our server authorization key?? – Mohanraj Sep 20 '17 at 06:12
  • push body { data = { audioUrl = ""; desc = noti; endTime = "22-09-2017 11:45:45"; imgUrl = ""; location = chennai; startTime = "21-09-2017 11:45:38"; timenumber = 1379; title = push; }; notification = { body = noti; title = push; }; to = "eylR3hervg0:APA91bHmMnY8ZSutyQp-K5s3-u5NzTiLxsGkQBku1t3lZSuzM9rGiQFT0WKv7A4KrqOWpIJVuRyRPPmNVldsxHLolXRGZBXHigdf4Tfn_BTPW0buedNpGq1R5icuJAZ3H734YeMNCSeh"; } – Mohanraj Sep 20 '17 at 06:40
  • this is my payload and i get UserInfo={NSDebugDescription=Invalid value around character 0.} error – Mohanraj Sep 20 '17 at 06:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154861/discussion-between-mohanraj-and-prateek-varshney). – Mohanraj Sep 20 '17 at 07:17
-1

Old post but it's worth to add to accepted solution as it can be helping others. You can check my post Push Notifications are delivered but didReceiveRemoteNotification is never called Swift or here didReceiveRemoteNotification function doesn't called with FCM notification server. You must have "content_available": truein your notification definition or push notification service(with underscore) or your didReceiveRemoteNotification won't be called and you won't be able to use userInfo.

Vincenzo
  • 5,304
  • 5
  • 38
  • 96