Local Notification Tutorial

11
Local Notification Tutorial "Android Application Development Company India" www.letsnurture.com

description

Learn how to setup local notification

Transcript of Local Notification Tutorial

Page 1: Local Notification Tutorial

Local Notification Tutorial

"Android Application Development Company India" www.letsnurture.com

Page 2: Local Notification Tutorial

Android Notifications

Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.

Here we have used NotificationCompat because on older platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.

For example, action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later. So provide support support lib v4

follow below step to create Notification with different style.

Step 1: Create Notification Builder

Create Notification Builder using NotificationCompat.Builder.build(). NotificationCompat.Builder. Used for set various Notification Properties like its small and large icons,title priority ...

// Building the notification

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

Step 2 : Setting Notification Properties

Once you have Builder object, you can set its Notification properties using Builder object as per your requirement. But this is mandatory to set at least following:A small icon, set by setSmallIcon()A title, set by setContentTitle()Detail text, set by setContentText()

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(

context).setSmallIcon(R.drawable.ic_launcher)

.setContentTitle("simple notification")

"Android Application Development Company India" www.letsnurture.com

Page 3: Local Notification Tutorial

.setContentText("the text of the simple notification")

Step 3 - Attach Action to Notification

This is an optional part and required if you want to attach an action with the notification. An action allows users to redirecte from the notification to an Activity in application, where they can look at one or more events or do further work.The action can be achived by a PendingIntent containing an Intent that starts an Activity in your application. To associate the PendingIntent with a gesture, call the appropriate method of NotificationCompat.Builder. For example, if you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().

// Pending intent to the notification manager

PendingIntent resultPending = stackBuilder.getPendingIntent(0,

PendingIntent.FLAG_UPDATE_CURRENT);

// Building the notification

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(

context).setSmallIcon(R.drawable.ic_launcher)

.setContentTitle("simple notification")

.setContentText("the text of the simple notification")

.setContentIntent(resultPending);// attach action here.

Step 4 : Issue the notifacation

You pass the Notification object to the system by calling NotificationManager.notify() to send your notification. Make sure you call NotificationCompat.Builder.build() method on builder object before notifying it. This method combines all of the options that have been set and return a newNotification object.

// mId allows you to update the notification later on.NotificationManager mNotificationManager.notify(10, mBuilder.build());

This Method post a notification to be shown in the status bar. If a notification with the same id has already been posted by application and has not yet been

"Android Application Development Company India" www.letsnurture.com

Page 4: Local Notification Tutorial

canceled, it will be replaced by the updated information.

Set Notification Priority

You can set the priority of a notification. The priority acts as a hint to the device UI about how the notification should be displayed. To set a notification's priority, callNotificationCompat.Builder.setPriority() and pass in one of the NotificationCompat priority constants. There are five priority levels, ranging from PRIORITY_MIN (-2) to PRIORITY_MAX (2); if not set, the priority defaults to PRIORITY_DEFAULT (0).

Here we have created differet kind of Notification.

Create a big view style to a notification

To Create Notification in a big view when it's expanded, first create a NotificationCompat.Builder object with the normal view. Next, call Builder.setStyle() with a big view style object as its argument.

Note: expanded notifications are not available on platforms prior to Android 4.1.how to handle notifications for Android 4.1 and for earlier platforms, read the section Handling compatibility.

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

// Building the expandable content

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

String lorem = context.getResources()

.getString(R.string.long_lorem);

String[] content = lorem.split("\\.");

inboxStyle.setBigContentTitle("This is a big title");

for (String line : content) {

inboxStyle.addLine(line);

}

// Building the notification

"Android Application Development Company India" www.letsnurture.com

Page 5: Local Notification Tutorial

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(

context).setSmallIcon(R.drawable.ic_launcher)

.setContentTitle("Expandable notification")

.setContentText("An example of an expandable notification")

.setStyle(inboxStyle);

mNotificationManager.notify(11, nBuilder.build());

} else {

Toast.makeText(context, "Can't show", Toast.LENGTH_LONG).show();

}

Create Progress in a Notification

To display a determinate progress bar, add the bar to your notification by calling setProgress() setProgress(max, progress, false) and then issue the notification. As your operation proceeds, increment progress, and update the notification. At the end of the operation, progress should equal max. A common way to call setProgress() is to set max to 100 and then increment progress as a "percent complete" value for the operation.

You can either leave the progress bar showing when the operation is done, or remove it. In either case, remember to update the notification text to show that the operation is complete. to remove the progress bar, call setProgress() setProgress(0, 0, false).

// used to update the progress notification

final int progresID = new Random().nextInt(1000);

// building the notification

final NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)

.setSmallIcon(R.drawable.ic_launcher)

.setContentTitle("Progres notification")

.setContentText("Now waiting")

.setTicker("Progress notification created")

"Android Application Development Company India" www.letsnurture.com

Page 6: Local Notification Tutorial

.setUsesChronometer(true).setProgress(100, 0, true);

AsyncTask<Integer, Integer, Integer> downloadTask = new AsyncTask<Integer, Integer, Integer>() {

@Override

protected void onPreExecute() {

super.onPreExecute();

mNotificationManager.notify(progresID, nBuilder.build());

}

@Override

protected Integer doInBackground(Integer... params) {

try {

// Sleeps 2 seconds to show the undeterminated progress

Thread.sleep(5000);

// update the progress

for (int i = 0; i < 101; i += 5) {

nBuilder.setContentTitle("Progress running...")

.setContentText("Now running...")

.setProgress(100, i, false)

.setSmallIcon(R.drawable.ic_launcher)

.setContentInfo(i + " %");

// use the same id for update instead created another

// one

mNotificationManager.notify(progresID, nBuilder.build());

Thread.sleep(500);

}

} catch (InterruptedException e) {

e.printStackTrace();

}

return null;

"Android Application Development Company India" www.letsnurture.com

Page 7: Local Notification Tutorial

}

@Override

protected void onPostExecute(Integer integer) {

super.onPostExecute(integer);

nBuilder.setContentText("Progress finished :D")

.setContentTitle("Progress finished !!")

.setTicker("Progress finished !!!")

.setSmallIcon(R.drawable.ic_launcher)

.setUsesChronometer(false);

mNotificationManager.notify(progresID, nBuilder.build());

}

};

// Executes the progress task

downloadTask.execute();

Now add Buttons to Notification Like Alarma Notification have two button one foe snoose and another for stop.

Follow bellow steps to add buttons.

Step 1: simply create simple notification as above discription.

Step 2: use this method to add action when creating notification NotificationCompat.Builder addAction (int icon, CharSequence title, PendingIntent intent);

Actions are typically displayed by the system as a button adjacent to the notification content.

Note: Action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.

"Android Application Development Company India" www.letsnurture.com

Page 8: Local Notification Tutorial

See bellow example for add buttons.

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

// Prepare intent which is triggered if the notification button is

// pressed

Intent intent = new Intent(context, TestActivity.class);

PendingIntent pIntent = PendingIntent.getActivity(context, 0,

intent, 0);

// Building the notification

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(

context).setSmallIcon(R.drawable.ic_launcher)

.setContentTitle("Button notification")

.setContentText("Expand to show the buttons...")

.setTicker("Showing button notification")

// action added here with pending intent.

.addAction(R.drawable.ic_launcher, "Accept", pIntent)

.addAction(R.drawable.ic_launcher, "Cancel", pIntent);

mNotificationManager.notify(1001, nBuilder.build());

} else {

Toast.makeText(context, "You need a higher version",

Toast.LENGTH_LONG).show();

}

Link of Source Code

"Android Application Development Company India" www.letsnurture.com

Page 9: Local Notification Tutorial

"Android Application Development Company India" www.letsnurture.com