Display Notification - Android Studio

How to Display Notification in android using Android Studio?

In this tutorial we will learn how to display a notification when button is clicked. When button is clicked the notification will be displayed. When the notification is clicked we will open some Activity.

Step 1: Create a new project OR Open your project

Step 2: Place an icon in drawable folder

Step 3: Code

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity" >

   <Button
       android:id="@+id/btnShowNotif"
       android:text="Show Notification"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java
package com.blogspot.devofandroid.myapplication;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button mBtnShowNoti;
    NotificationCompat.Builder mBuilder;
    NotificationManager mNotificationManager;
    PendingIntent mResultPendingIntent;
    TaskStackBuilder mStackBuilder;
    Intent mResultIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnShowNoti = findViewById(R.id.btnShowNotif);

        mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
        mBuilder.setContentTitle("Notification Title");
        mBuilder.setContentText("This is Notification detail...");

        mResultIntent = new Intent(this, MainActivity.class);
        mStackBuilder = TaskStackBuilder.create(this);
        mStackBuilder.addParentStack(MainActivity.class);

        // Adds the Intent that will start the Activity to the top of stack
        mStackBuilder.addNextIntent(mResultIntent);
        mResultPendingIntent = mStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(mResultPendingIntent);

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

        mBtnShowNoti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // notificationID allows you to update the notification later on.
                mNotificationManager.notify(1, mBuilder.build());
            }
        });

    }
}

Step 4: Output

 

Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

How to add AIDL folder | Android Studio