How to Show Notification in Android App in Android Studio | Latest Beginners Tutorials 2022

How to Show Notification in Android App in Android Studio | Latest Beginners Tutorials 2022





Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.



Display Notification - Android Studio







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.usmtip.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.view.View;
import android.widget.Button;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;


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());
            }
        });

    }
}






MainActivity.java      (2)


package com.usmtip.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.util.Log;
import android.view.View;
import android.widget.Button;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;


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);

        mBtnShowNoti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // notificationID allows you to update the notification later on.
                displayNotification();
            }
        });

    }


    protected void displayNotification() {
        Log.i("Start", "notification");
        int numMessages = 0;

        /* Invoking the default notification service */
        NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setContentTitle("New Message");
        mBuilder.setContentText("You have received new message.");
        mBuilder.setTicker("New Message Alert...!");
        mBuilder.setSmallIcon(R.drawable.icon);

        /* Increase notification number every time a new notification arrives */
        mBuilder.setNumber(++numMessages);

        /* Add Big View Specific Configuration */
        NotificationCompat.InboxStyle mInboxStyle = new NotificationCompat.InboxStyle();

        String[] mEvents = new String[4];
        mEvents[0] = new String("This is the 1st line...");
        mEvents[1] = new String("This is the 2nd line...");
        mEvents[2] = new String("This is the 3rd line...");
        mEvents[3] = new String("This is the 4th line...");

        // Sets a title for the Inbox style big view
        mInboxStyle.setBigContentTitle("Big Title Detail:");

        // Moves mEvents into the big view
        for (int i=0; i < mEvents.length; i++) {
            mInboxStyle.addLine(mEvents[i]);
        }

        mBuilder.setStyle(mInboxStyle);

        /* Creates an explicit intent for an Activity in your app */
        Intent resultIntent = new Intent(this, MainActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);

        /* Adds the Intent that starts the Activity to the top of the stack */
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        /* notificationID allows you to update the notification later on. */
        mNotificationManager.notify(0, mBuilder.build());
    }

}












All Server Links to Download This File:



Previous Post
Next Post

post written by:

0 Comments: