25 July 2015

Make your own Android Notification

What we will do?
-> We will make an app with a button. When the button is pressed we will get a notification.

What we will need?
  • activity_main.xml
  • MainActivity.java
Step 1 : Create a new android application. Click on File->New->Android Application Project.
file new android app

Step 2 : Give the name as NotificationDemo and package name as com.mia.notificationdemo. Click Next continuously.
NotificationDemo mia
Click Finish.
click finish

Step 2 : Add a button in activity_main.xml and change its name to any name(Notification).
drag button
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <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/editText3"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />

</RelativeLayout>

Step 3 : The code for MainActivity.java is shown below.

MainActivity.java
package com.mia.notificationdemo;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

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

Button b1=(Button)findViewById(R.id.button);
   
     b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         
           NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
           Notification notify=new Notification(R.drawable.ic_launcher,"Title",System.currentTimeMillis());
           PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
           notify.setLatestEventInfo(getApplicationContext(),"Subject","Body",pending);
           notif.notify(0, notify);
        }
     });
}
}

Step 4 : Run the application.
notification output 1
notofication output 2

Explanation of the code :

Explanation from MainActivity.java :
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  • Then we define the Notification object notify with icon,title and time of the notification. The title is not actually displayed in the notification. It is just used as a unique name to the notification.
Notification notify=new Notification(R.drawable.ic_launcher,"Title",System.currentTimeMillis());
  • Then we use object of type PendingIntent named pending. PendingIntent is used when we launch another Intent(of the notification) after an action in our main Intent(Intent of MainActivity class).
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
  • The method getActivity() here creates a new Intent(for notification).
  • After that we use setLatestEventInfo() method to set the information in the latest notification. Here, in this example, the time is only changed and other parameters like head, body remains same.
notify.setLatestEventInfo(getApplicationContext(),"Subject","Body",pending);
  • At last we display the notification by calling the notify() method.
notif.notify(0, notify);

Github download
Stay Tuned with Made In Android

Previous Page Next Page Home

No comments:

Post a Comment

Top