🔥 Implement pure APNS to Firebase token converter
Now you can throw away Firebase libs from dependencies of your iOS apps because you can send pure APNS tokens to your server and it will register it in Firebase by itself.
It is must have for developers who don't want to add Firebase libs into their apps, and especially for iOS projects who use Swift Package Manager cause Firebase doesn't have SPM support for its libs yet.
How to use
Preparation
- Go to Firebase Console -> Project Settings -> Cloud Messaging tab
- Copy
Server Key
fromProject Credentials
area
Next steps are optional
- Put server key into environment variables as
FCM_SERVER_KEY=<YOUR_SERVER_KEY>
- Put your app bundle identifier into environment variables as
FCM_APP_BUNDLE_ID=<APP_BUNDLE_ID>
Tokens registration
/// The simplest way
/// .env here means that FCM_SERVER_KEY and FCM_APP_BUNDLE_ID will be used
application.fcm.registerAPNS(.env, tokens: "token1", "token3", ..., "token100").flatMap { tokens in
/// `tokens` is array of `APNSToFirebaseToken` structs
/// which contains:
/// registration_token - Firebase token
/// apns_token - APNS token
/// isRegistered - boolean value which indicates if registration was successful
}
/// instead of .env you could declare your own identifier
extension RegisterAPNSID {
static var myApp: RegisterAPNSID { .init(appBundleId: "com.myapp") }
}
/// Advanced way
application.fcm.registerAPNS(
appBundleId: String, // iOS app bundle identifier
serverKey: String?, // optional server key, if nil then env variable will be used
sandbox: Bool, // optional sandbox key, false by default
tokens: [String], // an array of APNS tokens
on: EventLoop? // optional event loop, if nil then application.eventLoopGroup.next() will be used
).flatMap { tokens in
/// the same as in above example
}