Search code examples
swiftfirebasefirebase-cloud-messagingapple-push-notifications

send notification from the client - Firebase Cloud Messaging - swift programmatically


Is it possible to find a push notification from the client using swift? I searched online and the only examples, documentation I found were strictly related to the use of Firebase Cloud Functions and or using the Firebase Console.

I wanted to know if it's possible to create and send a notification using Firebase Cloud Messaging from the client.


Solution

  • It's actually possible using the firebase rest api, the following endpoint could be used to send a notification:

    https://fcm.googleapis.com/fcm/send

    The HTTP request must be a POST request with these headers:

    Authorization: key=<server_key>

    Content-Type: application/json

    and the following body:

    {
      "to" : "<FCM TOKEN>",
      "collapse_key" : "type_a",
      "priority": 10,
      "data" : {
        "body" : "Notification",
        "title": "Notification title"
        }
    }
    

    Of course you need to know to the FCM token of the target device that should receive the notification or at least the topic where the device/user is subscribed to. If you want to send notification to a specific topic use the following body:

    {
      "to" : "/topics/<YOUR_TOPIC>",
      "collapse_key" : "type_a",
      "priority": 10,
      "data" : {
       "body" : "Notification",
       "title": "Notification title"
       }
    }