📱 Send Mobile Push Notifications with Android
This guide will walk you through sending push notifications to your Android application using NotificationAPI and Firebase Cloud Messaging (FCM).
Overview
Sending push notifications to Android devices is exclusively handled by Google’s Firebase Cloud Messaging (FCM). Implementing this from scratch requires significant backend infrastructure to manage device tokens and communicate with FCM.
NotificationAPI simplifies this process by managing the entire backend complexity for you. Our Android SDK handles device token registration, and our service sends the notifications through FCM on your behalf.
The setup process is as follows:
- Set up FCM
- Configure your Android project
- Install and initialize the NotificationAPI Android SDK
- Send a notification
1. Set up FCM
To send a push notification to your mobile app on an Android device, we need some credentials from your Firebase Cloud Messaging account. Below are the required steps to generate and save FCM credentials.
- Create a Firebase project here
- Go to Project Settings
- Click “Service Accounts”
- Click “Generate new private key”
- Copy the private key JSON
- Paste the private key JSON into the FCM Provider Configuration
- Click “Save”
2. Configure your Android project
Add the JitPack repository in your build.gradle
.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then add the Android SDK dependency.
dependencies {
implementation 'com.github.notificationapi:notificationapi-android-sdk:1.0.0'
}
3. Install and initialize the NotificationAPI Android SDK
Our Android SDK makes it easy to register the device for push notifications.
Create a new service that extends from NotificationApiService
class MyService: NotificationApiService()
Add your service to AndroidManifest.xml
<service
android:name=".MyService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Create a new activity that extends from NotificationApiActivity
class MainActivity : NotificationApiActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Configure NotificationAPI with credentials
NotificationApi.shared.configure(
NotificationApiCredentials(
clientId = "CLIENT_ID",
userId = "USER_ID"
)
)
// Request the user's permission to use notifications
NotificationApi.shared.askNotificationPermissions()
}
}
This will automatically handle requesting push permissions and registering the device token with NotificationAPI.
Parameters
Parameter | Type | Description |
---|---|---|
clientId* | string | Your NotificationAPI account clientId. You can get it from here. |
userId* | string | The unique ID of the user in your system. |
4. Send a notification
With the SDK initialized, you can now trigger notifications from your backend using something like cURL, or one of our backend SDKs. The userId
must match the one used to initialize the SDK.
Schematic Diagram
This diagram illustrates the entire flow, from app initialization to receiving a notification.
Frequently Asked Questions (FAQs)
Why are my notifications not arriving?
There can be several reasons:
- FCM Setup: Double-check that your FCM key is correctly uploaded to the NotificationAPI dashboard.
- User ID Mismatch: Ensure the
userId
in your Android app is identical to the one you’re sending to in the backend API call. - Device/Emulator Issues: Ensure your device or emulator has Google Play Services installed and is connected to the internet.
- App State: On some Android versions, notification delivery behavior can change if the app is in the foreground, background, or terminated. Check Google’s documentation for details.