It's a swift lib that gives ability to send push notifications through Firebase Cloud Messaging.
Built for Vapor3 and depends on JWT
Vapor lib.
Note: the project is in active development state and it may cause huge syntax changes before v1.0.0
If you have great ideas of how to improve this package write me (@iMike) in Vapor's discord chat or just send pull request.
Hope it'll be useful for someone :)
Edit your Package.swift
//add this repo to dependencies
.package(url: "https://github.com/MihaelIsaev/FCM.git", from: "0.2.0")
//and don't forget about targets
//"FCM"
First of all you should configure FCM in configure.swift
import FCM
/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
//here you should initialize FCM
}
let fcm = FCM()
services.register(fcm, as: FCM.self)
and don't forget to pass the following environment variables
fcmServiceAccountKeyPath // /tmp/serviceAccountKey.json
OR
fcmEmail // firebase-adminsdk-0w4ba@example-3ab5c.iam.gserviceaccount.com
fcmKeyPath // /tmp/fcm.pem
fcmProjectId // example-3ab5c
let fcm = FCM(pathToServiceAccountKey: "/tmp/serviceAccountKey.json")
services.register(fcm, as: FCM.self)
OR
let fcm = FCM(email: "firebase-adminsdk-0w4ba@example-3ab5c.iam.gserviceaccount.com",
projectId: "example-3ab5c",
pathToKey: "/tmp/fcm.pem")
services.register(fcm, as: FCM.self)
OR
let fcm = FCM(email: "firebase-adminsdk-0w4ba@example-3ab5c.iam.gserviceaccount.com",
projectId: "example-3ab5c",
key: "<YOUR PRIVATE KEY>")
services.register(fcm, as: FCM.self)
β οΈ TIP:serviceAccountKey.json
you could get from Firebase Consoleπ Just go to Settings -> Service Accounts tab and press Create Private Key button in e.g. NodeJS tab
Then you could send push notifications using token, topic or condition.
Here's an example route handler with push notification sending using token
router.get("testfcm") { req -> Future<String> in
let fcm = try req.make(FCM.self)
let token = "<YOUR FIREBASE DEVICE TOKEN>"
let notification = FCMNotification(title: "Vapor is awesome!", body: "Swift one love! β€οΈ")
let message = FCMMessage(token: token, notification: notification)
return try fcm.sendMessage(req.client(), message: message)
}
fcm.sendMessage
returns message name like projects/example-3ab5c/messages/1531222329648135
FCMMessage
struct is absolutely the same as Message
struct in Firebase docs https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
So you could take a look on its source code to build proper message.