How to generate a notification in an Android device?

In this article, we are going to generate a push notification in our android device with the help of NotificationManager class.
Submitted by Manu Jemini, on February 09, 2018

In the example below, we are going to use the NotificationManager and Notification class to create and show the notification on the screen.

To implement this in your program you will need to import in java file:

    android.app.Notification;
    android.app.NotificationManager;

Then create a layout file with three EditText and Button. In the Button above we have put an onClick property and set it to notify function which is in the java File.

First we will take the reference of the EditTexts and Button from the layout file. After that we will make a new instance of the NotificationManager.

Then when ever user clicks the button we take the value of the EditText and convert it into the string and use it inside the NotificationManager. Like this:

Notification notify = new Notification.Builder(getApplicationContext()). setContentTitle(t).setContentText(b). setContentTitle(s).setSmallIcon(R.drawable.includehelp).build();

In the end, we would add a flag to the Notification object and show the notification on the screen.

1) Java file:

package com.example.hp.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity
{

    EditText T,S,B;
    String t,s,b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        T=(EditText)findViewById(R.id.e1);
        S=(EditText)findViewById(R.id.e2);
        B=(EditText)findViewById(R.id.e3);

    }
    public void notify(View v)
    {
        t=T.getText().toString().trim();
        s=S.getText().toString().trim();
        b=B.getText().toString().trim();
        NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify=new Notification.Builder(getApplicationContext()).setContentTitle(t).setContentText(b).setContentTitle(s).setSmallIcon(R.drawable.includehelp).build();
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        notif.notify(0, notify);
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/e1"
        android:hint="Name"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/e2"
        android:hint="Subject"
        android:layout_below="@+id/e1"
        android:layout_alignLeft="@+id/e1"
        android:layout_alignStart="@+id/e1"
        android:layout_alignRight="@+id/e1"
        android:layout_alignEnd="@+id/e1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/e3"
        android:hint="Body"
        android:layout_below="@+id/e2"
        android:layout_alignLeft="@+id/e2"
        android:layout_alignStart="@+id/e2"
        android:layout_alignRight="@+id/e2"
        android:layout_alignEnd="@+id/e2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification"
        android:id="@+id/button"
        android:layout_marginTop="77dp"
        android:layout_below="@+id/e3"
        android:onClick="notify"/>


</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

generate a notification in an Android device 1
generate a notification in an Android device 2

generate a notification in an Android device 3


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.