Tuesday, 20 February 2018

How to Implement Notification in Android

Notification
notification is a message you can display to the user outside of your application's normal UI.   
In this tutorial we'll learn how to implement Notification in your app.Here we'll Popup a notification on Button Click
Create a new project in Android Studio
1. Open main_activity.xml and create a Button.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.minixithub.notificationturorial.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Notification"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
2.Open MainAcitvity.java and create a method to Popup a Local Notification.
package com.minixithub.notificationturorial;

import android.app.NotificationManager;
import android.app.PendingIntent;
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 {

    private Context context;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addNotification();
            }
        });
    }

//  Method To Display Notification
    private void addNotification(){
        NotificationCompat.Builder builder= new  NotificationCompat
               .Builder(context);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("TextNotification")
                .setContentText("This is Notification");
//        Creating Intent
        Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                      0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//        Adding Pending Intent to Builder
        builder.setContentIntent(pendingIntent);

//        Add Notification
        NotificationManager manager= (NotificationManager)
               context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0,builder.build());
    }
}

No comments:

Post a Comment