Swiftpack.co - Package - iMac0de/Perfect-FCM-Server

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: "https://github.com/iMac0de/Perfect-FCM-Server.git", "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.name = ""
			self.token = ""
			self.brand = ""
			self.model = ""
			self.os = ""
			self.version = ""

		init(name: String, token: String, brand: String, model: String, os: String, version: String) {
			self.name = 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 (test.sh for exemple):

    swift test

Apply the execution right to the file:

    chmod +x test.sh

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!".


Stars: 0
Help us keep the lights on

Used By

Total: 0


1.0.2 - Feb 12, 2018

1.0.1 - Feb 11, 2018

1.0.0 - Feb 5, 2018