Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Notification From Specific Device To Specific Device Using Firebase iOS Swift

I would be very thankful for help with Push Notifications. My app has a chat where users can send text messages directly to each other. But without Push Notifications it doesn't make much sense. It is all set up on Firebase. How could I send Push Notifications from a specific device to a specific device? I did try Firebase Notifications and OneSignal due to a recommendation on Stackoverflow. But I am only able to trigger the notifications from Firebase or OneSignal directly and not from a specific device to a specific device.

Does someone has experience with this?

like image 581
NilsBerni Avatar asked Oct 31 '17 17:10

NilsBerni


People also ask

Can I use Firebase for push notification to iOS?

Firebase Cloud Messaging integrates with the Apple Push Notification service (APNs), however APNs only works with real devices.

How do I send notifications from Firebase console to specific user?

For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.


1 Answers

If your data is being stored in Firebease I would use Cloud Messaging for Push notifications as well. There are multiple ways to achieve this messaging notification functions, I will resume the one I think is the easiest.

Assuming you are using FCM and you have configured your app for it.

First of all you need to store the token of the users device, this would be stored as a string with each of the users info:

let token = Messaging.messaging().fcmToken

So in your Firebase DB, in each of your users object data you would have a key storing a string with the token, if you want the user to receive notifications in multiple devices, you must store the multiple tokens of the user using an array of strings, or even an array of objects and store the device type, etc. Thats your choice and your needs.

The ideal push notification environment is usually composed by a middle server that receive the request and manage and send the notification to the corresponding users, in this case you can skip this and send the not directly from your app, using FCM POST request service:

Example of how to build a FCM POST request to send a notification:

let url = URL(string: "https://fcm.googleapis.com/fcm/send")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=--HERE-GOES-YOUR-API-KEY--", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"

Here you put all not data, read the docs to understand all the keys and variables that you can send, yo would need to make a previous firebase request in which you get the device tokens.

var notData: [String: Any] = [
    "to" : "HERE YOU PUT THE DEVICE TOKEN(s)",
        "notification": [
          title : "not title",
          body  : "not body",
          icon  : "not icon"
        ],
        "data": [
          //More notification data.
      ]
]

And then you send the post request, It will return an object if the notification was succeed and more data.

request.httpBody = notData.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data, error == nil else {                                                 // check for fundamental networking error
        print("error=\(error)")
        return
    }

    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
        print("statusCode should be 200, but is \(httpStatus.statusCode)")
        print("response = \(response)")
    }

    let responseString = String(data: data, encoding: .utf8)
    print("responseString = \(responseString)")
}
task.resume() 

I've been using this same solution for a mobile native app and a react web app and it works like a charm.

like image 174
Karlo A. López Avatar answered Sep 22 '22 07:09

Karlo A. López