Notification In Android

Notification In Android Example

In this tutorial we will learn how to implement Android push notification with a simple example.

A notification is a message that you can display the user outside the Application. In this example we will show a notification and on click of the notification we will launch another activity.

Example:
  • Create a new project in Android Studio and name it as Notification.
  • Create a new Activity and name it as Main2Activity.
  • Design a new Button in main layout as follows.
File: activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="createNotification"
        android:text="Create Notification" >

    </Button>

</LinearLayout>
  • Do the following code in Main java file as follows.
File: MainActivity.java:
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void createNotification(View view) {
        // Prepare intent which is triggered if the notification is selected
        Intent intent = new Intent(this, Main2Activity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

        // Build notification
        Notification noti = new Notification.Builder(this)
                .setContentTitle("Notification from " + "androidlovers@gmail.com")
                .setContentText("AndroidLovers").setSmallIcon(R.drawable.bell)
                .setContentIntent(pIntent)
                .addAction(R.drawable.phone, "Call", pIntent)
                .addAction(R.drawable.more, "More", pIntent)
                .addAction(R.drawable.info, "Info", pIntent).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after it is selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, noti);

    }
}

  • Do the following things in Main2Activity files.
File: activity_main2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" >

    <TextView
        android:text="ANDROID LOVERS"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textSize="24sp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:text="Welcome to Android lovers" >
    </TextView>

</LinearLayout>
File: Main2Activity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

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

  • Run the App and enjoy the notification.
Output:


Download project
Thank You!!!
Please like and share...


Comments