Home > Java > javaTutorial > body text

Android Push Notifications Step by Step Guide

Linda Hamilton
Release: 2024-11-18 08:46:02
Original
993 people have browsed it

Push notifications play an important role in user engagement and retention in your mobile app, keeping users up to date and creating a sense of urgency that leads to purchases. And in Android, we get the added benefit of Firebase Cloud Messaging (FCM) notification service, which acts as a middleman between the app server and the user’s Android device. It helps the delivery of push notifications, even if the app is not active or the user is using a different app on their device.

Firebase Cloud Messaging (FCM) is used to send data messages and alerts to users, thanks to its robust cross-messaging system. iOS, Web and Android platforms are all supported by FCM. Messages with a data payload of up to 4kb can be sent using FCM push notifications.

In this article, we will explore the step-by-step integration of push notifications into Android apps, and discuss the critical aspects of implementation. We will also introduce several products and tools that can simplify and amplify user engagement.

Understanding Android Push Notifications

Mobile push notifications help to improve user engagement, customer retention, and overall user experience in your applications. When push notifications are used properly, they can significantly enhance the effectiveness of mobile apps across various business domains.

Push notifications can be used for different use cases and purposes in your Android app, including:

i) Updating a user on the status of their online booking or schedule.

ii) Provide clarifications and updates for an online order the user has placed.

iii) Advising users of changes in the back-end and helping them track their package.

iv) Creating hype around events linked to the app, and introducing a user to new offers, promotions and opportunities.

v) Encouraging users to try new app features with teasing messages and notifying them of server updates and other back-end changes.

Implementation Approach of Push Notifications in Android Application

Prerequisites

You need to have these tools and services in order to successfully implement push notifications in your Android app FCM:

i) Android Studio – This is the IDE used for the Android development.

ii) Basic Knowledge & Understanding – You need a basic understanding of Android project development and structure, and manifest file configuration.

iii) A Firebase Account – You should have a Firebase account for creating and managing your project settings.

iv) Internet Availability – Integration testing and firebase setup require an internet connection.

Step 1. Setup your Firebase Project

Visit Firebase Console.

Create your project by Giving it a name and Click continue.

Android Push Notifications Step by Step Guide

For further configuration, you can enable or disable the Google Analytics service to get the data analytics for your app.

To complete the setup you need to review the project settings; then you can create your project.

After you have created your project, you need to do additional project configuration for the push notification implementations.

Registering your app in project dashboard

You need to click on the Android icon in the dashboard to add an Android app. Next, enter a Android package name. This package name must be the same as the package name in your project (i.e., com.app.demoapp).

Android Push Notifications Step by Step Guide

You can also enter your app’s nickname to identify your app in the console.

Add SHA-1 key for your app. You need to use this command to generate your SHA-1 key in your terminal: Now copy the SHA-1 key and paste it into the Firebase console.

./gradlew signingReport

Copy after login
Copy after login
Copy after login

Finally Click ‘Register App’ to finish configuration.

Android Push Notifications Step by Step Guide

Setup Google Services

Once you finish registering your app, you can download the google-services.json file and save this it to your computer.

Now you need to add google-services.json to your Android project, file to the app directory of your Android project.

MyApp/
├── app/
│ ├── src/
│ ├── build.gradle
│ ├── google-services.json
├── build.gradle
├── settings.gradle

Copy after login
Copy after login
Copy after login

Add Firebase SDK

Next, add Firebase SDK to Your Project, in build.gradle add the Google services classpath:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

Copy after login
Copy after login
Copy after login

in app/build.gradle add the following dependencies:-

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    // ...
}

dependencies {
    // Add these lines
    implementation platform('com.google.firebase:firebase-bom:29.0.4')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging'
}

Copy after login
Copy after login
Copy after login

After you finish these steps you can click ‘Sync Now’ in the Android Studio to sync your project with the Firebase.

Step 2. Configure Your Android Project

Project-Level build.gradle

Open Your Project-Level build.gradle: Navigate to the root directory of your Android project and locate the build.gradle file.

Ensure the google() and mavenCentral() repositories are included in the repositories section.

buildscript {
    repositories {
        // Check that you have these lines
        google()
        mavenCentral()
    }
    dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
        // Check that you have these lines
        google()
        mavenCentral()
    }
}

Copy after login
Copy after login
Copy after login

Make changes in the build.gradle

Find and locate the build.gradle file in your project app directory, then add the google-services plugin at the end of the file, and add the Firebase and Firebase Messaging dependency in the dependencies section.

./gradlew signingReport

Copy after login
Copy after login
Copy after login

With these steps completed, your project is configured with Firebase dependencies, including Firebase Messaging for push notifications. Let us now proceed with Firebase Messaging service setup and handling the notifications in your app.

Step 3. Implement the FCM

Add a Service to Handle FCM Messages

Create a new class named MyMessagingService that extends FirebaseMessagingService.

You need to override onMessageReceived Method to handle incoming messages when your app is in foreground state. Then implement logic to handle the notification and optionally show it to the user.

MyApp/
├── app/
│ ├── src/
│ ├── build.gradle
│ ├── google-services.json
├── build.gradle
├── settings.gradle

Copy after login
Copy after login
Copy after login

Register in AndroidManifest.xml file

Open the AndroidManifest.xml file in your project.

Now you need to register the MyMessagingService in the manifest to handle FCM messages.

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

Copy after login
Copy after login
Copy after login

Step 4: Obtain the FCM Registration Token

To receive push notifications, your app needs to obtain the FCM registration token. This token uniquely identifies the app instance on the device.

Code Example to Fetch the Device Token

You can fetch the FCM registration token in your FirebaseMessagingService or any other appropriate place in your app. Let’s see how to do it in FirebaseMessagingService.

Update MyMessagingService:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    // ...
}

dependencies {
    // Add these lines
    implementation platform('com.google.firebase:firebase-bom:29.0.4')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging'
}

Copy after login
Copy after login
Copy after login

You can fetch the token in an Activity or Fragment, and store or use it as required.

buildscript {
    repositories {
        // Check that you have these lines
        google()
        mavenCentral()
    }
    dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
        // Check that you have these lines
        google()
        mavenCentral()
    }
}

Copy after login
Copy after login
Copy after login

Using these methods you can get the FCM registration token in your Android app. The token can be fetched either in the FirebaseMessagingService or any Activity or Fragment. This token is important for sending targeted push notifications to specific devices.

Step 5: Send Push Notifications

If you want to send notifications from a server you need to make a POST request to FCM API.

You need to Get a Server Key

  • In the Firebase Console → Project Settings → “Project Overview” → “Cloud Messaging”.
  • In “Project credentials”, you will find the Server key.

cURL Request: This cURL command will be used to send notifications. Replace with your actual server key and with the FCM token of the target device.

./gradlew signingReport

Copy after login
Copy after login
Copy after login
  • https://fcm.googleapis.com/fcm/send API endpoint.
  • H "Authorization: key=": This is an authorization header with your server key.
  • H "Content-Type: application/json": content type header.
  • d '{ ... }': This is the JSON payload that has your notification and data.

Advanced Topics

Handling Data Messages

Data messages are a type of message that can carry custom key-value pairs and are handled differently from notification messages. Data messages are received in the onMessageReceived method of FirebaseMessagingService , regardless of whether the app is in the foreground or background.

Example for Handling Data Messages

Update MyMessagingService

MyApp/
├── app/
│ ├── src/
│ ├── build.gradle
│ ├── google-services.json
├── build.gradle
├── settings.gradle

Copy after login
Copy after login
Copy after login

Managing Notification Channels

Starting with Android 8.0 (API level 26), all notifications must be assigned to a channel. This allows users to control notification settings for each channel.

Example for Creating Notification Channels

Create a Method to Set Up Notification Channels:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

Copy after login
Copy after login
Copy after login

Call This Method in Your MainActivity or Application Class:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    // ...
}

dependencies {
    // Add these lines
    implementation platform('com.google.firebase:firebase-bom:29.0.4')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging'
}

Copy after login
Copy after login
Copy after login

Alternatively, you can call NotificationUtils.createNotificationChannel(this); in your Application class if you have one:

buildscript {
    repositories {
        // Check that you have these lines
        google()
        mavenCentral()
    }
    dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
        // Check that you have these lines
        google()
        mavenCentral()
    }
}

Copy after login
Copy after login
Copy after login

Update Your Notification Builder to Use the Channel ID:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.example.myandroidapp"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    // Add Firebase BOM
    implementation platform('com.google.firebase:firebase-bom:29.0.4')
    // Add Firebase Messaging dependency
    implementation 'com.google.firebase:firebase-messaging'
}

// Add this line at the bottom of the file
apply plugin: 'com.google.gms.google-services'

Copy after login

Testing Push Notifications

When push notifications are implemented, we need to do testing to make sure they are implemented correctly.

In Android Studio, you can open the logcat, View > Tool Windows > Logcat.

We can also filter the logcat output using the tag we used in the FirebaseMessagingService class. The application must be running on device or through an emulator.

When the notification is received, we can see the data in logcat like this:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Handle the received message
        if (remoteMessage.getNotification() != null) {
            // Get the message body
            String messageBody = remoteMessage.getNotification().getBody();
            // Send a notification
            sendNotification(messageBody);
        }
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0, notificationBuilder.build());
    }
}

Copy after login

Message data can be seen like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myandroidapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- Add this service -->
        <service
            android:name=".MyMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <!-- Other activities and services -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Copy after login

Conclusion

In this article we learned about push notifications, along with a step-by-step approach to implementation. We learned how to set up a project in Firebase console and how to complete the required configuration in Firebase project settings, so you can start sending Android notifications to your Android users.

You need to start by setting up a Firebase project and downloading the google-services.json file. After that, you need to put this file app’s directory and modify the build.gradle files to include Firebase dependencies.

Then, you need to create a class that will handle the incoming messages and register this service class in the AndroidManifest.xml. After adding the service in manifest file, you need to fetch the FCM token that is used to uniquely identify your app, so a notification can be sent to the targeted device.

Notifications can be tested by sending messages from the Firebase Console and using Android Studio’s logcat to confirm delivery. To activate server-side notifications, use a cURL request with your Firebase server key and the device’s FCM token.

To maintain compatibility with later Android versions, you’ll need to process data messages in your ‘FirebaseMessagingService` and manage notification channels for sophisticated configurations.

Push notifications, which offer relevant updates and customized information – and can increase user retention and conversion rates – are crucial for encouraging user engagement.

Read the official Firebase and Android SDK docs for more information and in-depth directions. These websites provide thorough guidance and industry-best practices for incorporating push notifications into your application.

The above is the detailed content of Android Push Notifications Step by Step Guide. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template