SonicFCM is a lightweight Android library built on top of Firebase Cloud Messaging (FCM) that makes receiving and displaying push notifications extremely simple.
Follow the official Firebase guide: https://firebase.google.com/docs/android/setup
Make sure you have added: - google-services.json - Google Services
Gradle plugin - Firebase dependencies
id 'com.google.gms.google-services' version 'latest-version' apply falseid 'com.google.gms.google-services'Add in settings.gradle:
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}Add in app-level build.gradle: latest version
implementation 'com.github.orbitalsonic:SonicFCM:3.1.3'Call inside Application class or MainActivity:
SonicFcmManager.setupFcm(context, "YourTopicName")SonicFcmManager.removeFCM("YourTopicName")You can send notifications using:
- Firebase Console\
- Postman (HTTP v1 API)
- Enable Firebase Cloud Messaging API in Google Cloud Console\
- Generate
service-account.jsonfrom Service Accounts\ - Use it to generate access token
service-account.json publicly.
Add service-account.json inside the assets folder:
private fun setupFirebaseMessaging() {
if (BuildConfig.DEBUG) {
CoroutineScope(Dispatchers.IO).launch {
try {
val credentials = assets.open("service-accounts.json").use { inputStream ->
GoogleCredentials.fromStream(inputStream)
.createScoped(listOf("https://www.googleapis.com/auth/firebase.messaging"))
}
credentials.refreshIfExpired()
val token = credentials.accessToken.tokenValue
Log.i("accessTokenTAG", "ACCESS-TOKEN = $token")
} catch (e: Exception) {
Log.e("accessTokenTAG", "ACCESS-TOKEN = $e")
}
}
}
}This method is DEBUG ONLY.
- Do NOT commit
service-account.json - Do NOT ship it in production
- Use a secure backend server in production
- Delete the file after generating token
- NEVER upload it publicly
https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json
{
"message": {
"topic": "myTopicName",
"data": {
"type": "GENERAL",
"title": "Hello from SonicFCM",
"body": "This is a test notification",
"icon": "https://yourdomain.com/icon.png",
"feature": "https://yourdomain.com/banner.png"
}
}
}{
"message": {
"topic": "YourTopicName",
"data": {
"type": "EXTERNAL_LINK",
"title": "Hello from SonicFCM",
"body": "This is a test notification",
"icon": "https://yourdomain.com/icon.png",
"feature": "https://yourdomain.com/banner.png",
"url": "https://yourdomain.com"
}
}
}Starting from Android 13 (API level 33), apps must request notification permission at runtime in order to receive and display notifications. Without this permission, notifications will not appear on the device.
Copyright 2022 OrbitalSonic
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

