Search This Blog

Showing posts with label Notification. Show all posts
Showing posts with label Notification. Show all posts

Tuesday, 3 July 2012

Android 4.1 Rich Notifications


New Notification APIs in Android 4.1


Android 4.1 (Jelly Bean) expanded the notification framework Now the apps can create big and rich notifications those can be expand or collapse with a pinch or swipe. The notifications added new content types including photos and have a configurable priority.


The user could have multiple response for a status notification. Now with enhanced notification framework the android app developers can build really good notifications for the user.
  • BigTextStyle — a notification that includes a multiline TextView object.
  • BigInboxStyle — a notification that shows any kind of list such as messages, headlines, and so on.
  • BigPictureStyle — a notification that showcases visual content such as a bitmap.
In addition to the templated styles, you can create you own notification styles using any remote View.
Apps can add up to three actions to a notification, which are displayed below the notification content. The actions let the users respond directly to the information in the notification in alternative ways. such as by email or by phone call, without visiting the app.


With expandable notifications, apps can give more information to the user, effortlessly and on demand. Users remain in control and can long-press any notification to get information about the sender and optionally disable further notifications from the app.


The setStyle() API allow you to set the new style for the notifications. The multiple actions to the notification can be add using addAction() API.


The setPriority() method could be use to define the priority of the notification for the system.

Monday, 2 July 2012

Basics of Notifications in Andoird

Android provides the notification APIs for applications to push the notifications for the user on the device.

Toast Notification


The toast notifications invoke on the surface of the window.You can invoke the toast notifications from the Activity or Service.The Toast notifications acquire the area equivalent to the message and automatically fades. The user current activity is interactive upon the display of the toast notification.

The toast notification doesn't provide any interaction to the user. They are only useful to send the read only messages as notification to the user.

Create the simple toast notification in your activity class.

Context context = getApplicationContext();
CharSequence text = "Hi,I am a toast notification";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
 

Status Bar Notification

The status bar notification provides the interaction with the user. The notifications which need a response from the end user should add to the status bar notifications. A status notification add a icon to the system status bar and a notification message in the notification window.

When user selects the notification the android fires and Intent  to launch the Activity defined by notification.

The sound,vibration or flashing lights could also be used for notification.

The background services need to notify the event which needs user response in the status notification.

The status notifications are managed in android using Notification and NotificationManager classes. 

The Notification class defines the notification which includes the status icon, message and the action (Activity to start as response from user).

The Notification manager class manages the notification system service and handle all the status bar notifications in android.

To create a simple status notification you can follow the following steps

Get a instance of the Notification manager as follows

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =  
(NotificationManager) getSystemService(ns);

Now create a notification object as follows

int icon = R.drawable.notification_icon;
CharSequence tickerText = "StatusBar";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, LanuchAsResponse.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
 
Now notify the notification to the status bar as follows

private static final int SAMPLE_ID = 1;

mNotificationManager.notify(SAMPLE_ID, notification);