Notification in android
A notification is a message you can display to the user outside of your application's normal UI. For details click here
Follow the bellow steps to create notification in android .
Step 1: Create Project
a) Open Android Studio
b) Go to File >New> New Project > Project Name > Next > Next > Next > Finish
Step 2: Layout Design
open activity_main.xml file and add the bellow code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<Button
android:id="@+id/btnNoti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Notification"/>
</RelativeLayout>
Step 3:Create a class "MainActivity.java" which extends Activity class and add the bellow code.
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
Button btnNoti;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
btnNoti=(Button)findViewById(R.id.btnNoti);
// set Button onClickLister
btnNoti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// to get notification manager object from system using
// getSystemService() method.
NotificationManager notificationManager = (NotificationManager)
getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context);
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Apna Android");
mBuilder.setContentText("Notification Example");
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
else{
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setColor(Color.parseColor("#0286C9"));
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher);
mBuilder.setLargeIcon(notificationLargeIconBitmap);
}
mBuilder.setVisibility(Notification.PRIORITY_MAX);
mBuilder.setPriority(Notification.PRIORITY_HIGH);
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
});
}
}
Find Us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Output :
Comments
Post a Comment