📱 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:

  1. Set up FCM
  2. Configure your Android project
  3. Install and initialize the NotificationAPI Android SDK
  4. 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.

  1. Create a Firebase project here
  2. Go to Project Settings
  3. Click “Service Accounts”
  4. Click “Generate new private key”
  5. Copy the private key JSON
  6. Paste the private key JSON into the FCM Provider Configuration
  7. 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

ParameterTypeDescription
clientId*stringYour NotificationAPI account clientId. You can get it from here.
userId*stringThe 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.

sequenceDiagram participant User participant AndroidApp participant "NotificationAPI SDK" participant "Your Backend" participant NotificationAPI participant "Google (FCM)" User->>AndroidApp: Opens App & Logs In AndroidApp->>"NotificationAPI SDK": Initializes with Client ID & User ID "NotificationAPI SDK"->>"Google (FCM)": Requests Push Token "Google (FCM)"-->>"NotificationAPI SDK": Returns Push Token "NotificationAPI SDK"->>NotificationAPI: Sends User ID and Push Token "Your Backend"->>NotificationAPI: Triggers Notification for User ID NotificationAPI->>"Google (FCM)": Sends Notification Payload "Google (FCM)"->>AndroidApp: Delivers Push Notification AndroidApp->>User: Displays 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.