Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to Push Notification in Android?

Push notifications in Android can be implemented using several services, with Firebase Cloud Messaging (FCM) being one of the most popular. Here's how you can implement push notifications in an Android app using FCM:

1. Set up Firebase Cloud Messaging (FCM):

  1. If you haven't already, add Firebase to your Android project.

  2. In the Firebase console, go to your project settings and download the google-services.json file. Add this file to your app module's root directory in Android Studio.

  3. In your app's build.gradle file, add the necessary dependencies:

    implementation 'com.google.firebase:firebase-messaging:20.2.4'  // Check for the latest version
    

2. Create a Service for Receiving Messages:

  1. Create a service that extends FirebaseMessagingService. This service will handle the receipt of the push notifications:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
    
            // Handle FCM messages here. For example, create a notification based on the message's content.
            if (remoteMessage.getNotification() != null) {
                String title = remoteMessage.getNotification().getTitle();
                String body = remoteMessage.getNotification().getBody();
                showNotification(title, body);
            }
        }
    
        private void showNotification(String title, String body) {
            // Create and show a notification
        }
    }
    
  2. Register the service in the AndroidManifest.xml:

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    

3. Create Notifications:

Use the NotificationManager to display the received notifications. Make sure to create a notification channel if targeting Android Oreo (API level 26) and above.

4. Send a Test Notification:

  1. Go to the Firebase console.

  2. Navigate to the Cloud Messaging tab.

  3. Click on "Send your first message" or "Send a message".

  4. Fill in the necessary details and target the message to your app.

  5. Click "Review" and then "Publish" to send the notification.

Now, when you run your app, it should be able to receive and display push notifications sent from the Firebase Console.

Remember: To make the most of push notifications, always ensure the content is relevant and timely. Avoid sending too many notifications to prevent users from muting them or uninstalling your app.

  1. Android push notification example code:

    • Below is a simplified example of handling FCM push notifications:
    // Inside your FirebaseMessagingService
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            // Handle data payload
            String message = remoteMessage.getData().get("message");
            sendNotification(message);
        }
    
        if (remoteMessage.getNotification() != null) {
            // Handle notification payload
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();
            sendNotification(body);
        }
    }
    
    private void sendNotification(String messageBody) {
        // Create and show a notification
        // ...
    }
    
  2. Android notification channels and push notifications:

    • Android Notification Channels allow you to group notifications and give users more control over how they receive notifications. Implement channels in your NotificationCompat.Builder when creating notifications.
    // Creating a notification channel
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
            "channel_id",
            "Channel Name",
            NotificationManager.IMPORTANCE_DEFAULT
        );
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
    
    // Building a notification with a channel
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id");
    // ...
    
  3. Push notification libraries for Android development:

    • Apart from FCM, several libraries can simplify push notification integration. Examples include OneSignal, Pusher Beams, and Firebase Cloud Messaging (FCM) SDK.
    // OneSignal example dependency
    implementation 'com.onesignal:OneSignal:4.3.2'
    

    Follow the documentation of the chosen library for detailed integration instructions.