Notification android

ksheerodshritoshniwa 1,714 views 12 slides Jan 22, 2018
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

This is about Android Notification and in which you see how you create your own notification and default notification.


Slide Content

Notification : A service, running in the background, needs a way to let users know something of interest has occurred, such as when email has been received. We used toast to show details for some actions but they are not persistent to show for longer time. And another activity which is going on background. For important messages to be given to the user, it is required to have more persistent method. A notification is a message you can display as an icon at the top of the device which we call notification bar or status bar. To see the details of the notification, you will have to select the icon from the android phone, tablet or other devices which will display notification drawer having detail about the notification. we have to set Two type of notification view. 1. Normal View 2. Big View

Normal View : While working with emulator with virtual device, you will have to click and drag down the status bar to expand it which will give you detail as follows. This will be just 64 dp tall and called normal view. Big View : Big View which will have additional detail about the notification. You can add up to six additional lines in the notification. The following screenshot shows such notification.

Creating a Notification in android : For making or create a notification in android we have to do following steps : 1. Create Notification Builder 2. Setting Notification Properties 3. Attach Actions 4. Issue the notification Create Notification Builder : The first step is to create a notification builder using NotificationCompat.Builder.build () . You will use Notification Builder to set various Notification properties like its small and large icons, title, priority etc. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder (this);

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 () mBuilder.setSmallIcon ( R.drawable.notification_icon ); mBuilder.setContentTitle ("Notification Alert, Click Me!"); mBuilder.setContentText ("Hi, This is Android Notification Detail!"); Attach Actions : This is an optional part and required if you want to attach an action with the notification. An action allows users to go directly from the notification to an Activity in your application, where they can look at one or more events or do further work. The action is defined 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 ().

A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, without caring of whether or not your application is running. Example : Intent resultIntent = new Intent(this, ResultActivity.class ); TaskStackBuilder stackBuilder = TaskStackBuilder.create (this); stackBuilder.addParentStack ( ResultActivity.class ); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent ( resultIntent ); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent (0,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent ( resultPendingIntent );

Issue the notification : Finally, 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 new Notification object. NotificationManager mNotificationManager = ( NotificationManager ) getSystemService ( Context.NOTIFICATION_SERVICE ); // notificationID allows you to update the notification later on. mNotificationManager.notify ( notificationID , mBuilder.build ()); The NotificationCompat.Builder class allows easier control over all the flags, as well as help constructing the typical notification layouts. Following are few important and most frequently used methods available as a part of NotificationCompat.Builder class :

Notification build() : Combine all of the options that have been set and return a new Notification object. NotificationCompat.Builder setAutoCancel ( boolean autoCancel ) : Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. NotificationCompat.Builder setContent ( RemoteViews views) : Supply a custom RemoteViews to use instead of the standard one. NotificationCompat.Builder setContentInfo ( CharSequence info) : Set the large text at the right-hand side of the notification. NotificationCompat.Builder setContentIntent ( PendingIntentintent ) : Supply a PendingIntent to send when the notification is clicked. NotificationCompat.Builder setContentText ( CharSequence text) : Set the text (second row) of the notification, in a standard notification. NotificationCompat.Builder setContentTitle ( CharSequence title) : Set the text (first row) of the notification, in a standard notification.

NotificationCompat.Builder setDefaults ( int defaults) : Set the default notification options that will be used. NotificationCompat.Builder setLargeIcon (Bitmap icon) : Set the large icon that is shown in the ticker and notification. NotificationCompat.Builder setNumber ( int number) : Set the large number at the right-hand side of the notification. NotificationCompat.Builder setOngoing ( boolean ongoing) : Set whether this is an ongoing notification. NotificationCompat.Builder setSmallIcon ( int icon) : Set the small icon to use in the notification layouts. NotificationCompat.Builder setStyle ( NotificationCompat.Stylestyle ) : Add a rich notification style to be applied at build time. NotificationCompat.Builder setTicker ( CharSequence tickerText ) : Set the text that is displayed in the status bar when the notification first arrives.

NotificationCompat.Builder setVibrate (long[] pattern) : Set the vibration pattern to use. NotificationCompat.Builder setWhen (long when) : Set the time that the event occurred. Notifications in the panel are sorted by this time.

Push Notification : Push Notification is used to create your own notifications in android very easily. Android provides NotificationManager class for this purpose. In order to use this class, you need to instantiate an object of this class by requesting the android system through getSystemService () method. Its syntax is given below: NotificationManager NM; NM=( NotificationManager ) getSystemService ( Context.NOTIFICATION_SERVICE ); After that you will create Notification through Notification class and specify its attributes such as icon, title and time etc. Its syntax is given below: Notification notify=new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis()); The next thing you need to do is to create a PendingIntent by passing context and intent as a parameter. By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself. PendingIntent pending= PendingIntent.getActivity ( getApplicationContext (), 0, new Intent(),0);

The last thing you need to do is to call setLatestEventInfo method of the Notification class and pass the pending intent along with notification subject and body details. Its syntax is given below. And then finally call the notify method of the NotificationManager class. notify.setLatestEventInfo ( getApplicationContext (), subject, body,pending ); NM.notify (0, notify); Apart from the notify method, there are other methods available in the NotificationManager class. They are listed below: cancel( int id) : This method cancels a previously shown notification. cancel(String tag, int id) : This method also cancels a previously shown notification. cancelAll () : This method cancels all previously shown notifications.

notify( int id, Notification notification) : This method posts a notification to be shown in the status bar. notify(String tag, int id, Notification notification) : This method also Post a notification to be shown in the status bar.