← Back to list

How to Create and Display Notifications in Android Using Notification Channels (API 26+)

Learn how to implement a simple notification system in your Android app using Notification Channels for devices running API 26 (Android 8.0…

Chamika Gayashan · 2025-01-29 12:24 · 0 claps · 1.9 min read
#android-notification #android-studio #java-development #java #push-notification
Open on Medium ↗
Wiki topics: 🏃 · Running & Endurance

How to Create and Display Notifications in Android Using Notification Channels (API 26+)

How to Create and Display Notifications in Android Using Notification Channels (API 26+)

How to Create and Display Notifications in Android Using Notification Channels (API 26+)

Learn how to implement a simple notification system in your Android app using Notification Channels for devices running API 26 (Android 8.0 Oreo) and above. This article walks you through the code snippet that sets up a button click listener, creates a notification channel, and displays a basic notification with a title, text, and icon. Perfect for developers looking to enhance their app’s user experience with timely and relevant notifications.

Menifest permission

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        //API 26+
        NotificationManager notificationManager = getSystemService(NotificationManager.class);

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){

            NotificationChannel notificationChannel = new NotificationChannel(
                    "C1",
                    "Channel1",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            notificationManager.createNotificationChannel(notificationChannel);
        }
        //API 26+

        Notification notification = new NotificationCompat.Builder(MainActivity.this,"C1")
                .setContentTitle("Hello!")
                .setContentText("This is simple Notification")
                .setSmallIcon(R.drawable.outline_1x)
                .setPriority(Notification.PRIORITY_DEFAULT).build();

        notificationManager.notify(1,notification);

    }
});

Notification to move to another activity.

Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Intent intent = new Intent(MainActivity.this,MainActivity2.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                MainActivity.this,
                100,
                intent,
                PendingIntent.FLAG_IMMUTABLE
        );

        //API 26+
        NotificationManager notificationManager = getSystemService(NotificationManager.class);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel = new NotificationChannel(
                    "C1",
                    "Channel1",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            notificationManager.createNotificationChannel(notificationChannel);

            NotificationCompat.Action action = new NotificationCompat.Action.Builder(
                    R.drawable.outline_1x,
                    "View",
                    pendingIntent
            ).build();

            Notification notification = new NotificationCompat.Builder(MainActivity.this, "C1")
                    .setContentTitle("Hello!")
                    .setContentText("This is simple Notification")
                    .setSmallIcon(R.drawable.outline_1x)
                    .addAction(action)
                    .build();

            notificationManager.notify(1, notification);
        }
        //API 26+

    }
});

Send notification messages from Firebase Messaging.

Java Code

public class MessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {
        super.onMessageReceived(message);
        Log.i("App31Log",message.getNotification().getTitle());
        Log.i("App31Log",message.getNotification().getBody());

        NotificationManager notificationManager = getSystemService(NotificationManager.class);

        Notification notification = new NotificationCompat.Builder(getApplicationContext(),"C1")
                .setContentTitle(message.getNotification().getTitle())
                        .setContentText(message.getNotification().getBody())
                                .setSmallIcon(R.drawable.outline_1x)
                                        .build();

        notificationManager.notify(1,notification);
    }
}

Stylish Notification.

Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        EditText editText2 = findViewById(R.id.editText2);
        EditText editText3 = findViewById(R.id.editText3);

        String title = String.valueOf(editText2.getText());
        String message = String.valueOf(editText3.getText());

        if (title.isBlank()) {
            Toast.makeText(MainActivity2.this, "Please Enter Title!", Toast.LENGTH_SHORT).show();
        } else if (message.isBlank()) {
            Toast.makeText(MainActivity2.this, "Please Enter Message!", Toast.LENGTH_SHORT).show();
        } else {

            //API 26+
            NotificationManager notificationManager = getSystemService(NotificationManager.class);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                NotificationChannel notificationChannel = new NotificationChannel(
                        "C1",
                        "Channel1",
                        NotificationManager.IMPORTANCE_DEFAULT
                );

                notificationManager.createNotificationChannel(notificationChannel);
            }
            //API 26+

            Notification notification = new NotificationCompat.Builder(MainActivity2.this, "C1")
                    .setContentTitle(title)
                    .setContentText("Expand to View " + message)

                    //Add Large Icon to right side
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.image))

                    //Styles for expandable notification.
                    .setStyle(
                            new NotificationCompat.BigTextStyle()
                                    .bigText(message)
                    )

                    //Expand the image
                    .setStyle(
                            new NotificationCompat.BigPictureStyle()
                                    .bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.image))
                    )
                    .setSmallIcon(R.drawable.notify_icon)
                    .setPriority(Notification.PRIORITY_DEFAULT).build();

            notificationManager.notify(1, notification);
        }

    }
});

Good Luck & Thank You!

GitHub / LinkedIn / Facebook / Instagram

Follow Me On Medium

Follow Me On Medium


메타데이터
post_id
1920490b2bbb
slug
how-to-create-and-display-notifications-in-android-using-notification-channels-api-26-1920490b2bbb
url
https://medium.com/@chamikathereal/how-to-create-and-display-notifications-in-android-using-notification-channels-api-26-1920490b2bbb
canonical_url
https://medium.com/@chamikathereal/how-to-create-and-display-notifications-in-android-using-notification-channels-api-26-1920490b2bbb
author_url
https://medium.com/@chamikathereal
status
ok
fetched_at
2026-08-06 03:04:31