Experimental Firebase Cloud Messaging Server in Server Side Swift

What's New

Bug fix for Linux in debug mode


Perfect FCM Server


I have created this Perfect package in order to send PUSH notification to iOS and Android devices threw the Firebase Cloud Messaging system.

Compatibility with Swift

The master branch of this project currently compiles with Xcode 9.2 or the Swift 4.0 toolchain on Ubuntu.


Add this project as a dependency in your Package.swift file.

    .package(url: "", "1.0.0"..<"2.0.0")

How to use it?

Firebase Cloud Messaging server key

The first thing to do is to create a project on the Firebase console in order to retrieve a server key.


In order to use the Firebase API from your server side, you need to provide your server key to the Perfect FCM Server package.

    let config = PerfectFCM.Config(serverKey: YOUR_SERVER_KEY)

Send a notification

Once the Perfect FCM Server is configured, you should be able to send PUSH notification threw the Firebase Cloud Messaging system.

    do {
	    try PerfectFCM.send(config, to: "A_FCM_DEVICE_TOKEN_OR_TOPIC", title: "YOUR_TITLE", body: "YOUR_BODY", data: ["CUSTOM_KEY": "CUSTOM_DATA"])
    } catch {

It is possible to send the PUSH notification to a specific device by providing to the to parameter the device token retrieved with a Firebase client or to a topic aka /topics/daily-news where this topic send a PUSH notification each day to a group of devices for example.

Store the device token

It is up to you to create the logic to store the device token once you get it. In my case, I generally create a REST API that allow me to POST some data about the devices and save them in a database. Here is my Device class:

    class Device {
	    var name: String
	    var token: String
	    var brand: String
	    var model: String
	    var os: String
	    var version: String

		init() { = ""
			self.token = ""
			self.brand = ""
			self.model = ""
			self.os = ""
			self.version = ""

		init(name: String, token: String, brand: String, model: String, os: String, version: String) { = name
			self.token = token
			self.brand = brand
			self.model = model
			self.os = os
			self.version = version

Then you can collect these data on your client device and store them to a database of your choice.

You should know that a token is updated time-to-time for the same device. You should take that in account in order to be able to update the device in your database when the token has been changed.


If you want to test it, you can create the following script in a file ( for exemple):

    swift test

Apply the execution right to the file:

    chmod +x

And then run the script:


You should receive a PUSH notification to your device with the title "Hello world!" and the body "Perfect FCM PUSH notification!".


  • Swift Tools 4.0.0
View More Packages from this Author


Last updated: Tue Nov 08 2022 05:53:29 GMT-0500 (GMT-05:00)