Archive

Posts Tagged ‘NotificationService’

Android Basics – Notifications

January 28th, 2009

All the mobile application needs some background processing. Android applications are not exception to that. While the application is working in the background, user can go to other application on the phone. While working in the background, if something happens in the application (like an event) that requires user attention, the application should tell the user about the event. The application should not open some application screen (activity) in this case since your can be doing some other important task.
The solution for this is the notification. Notification allows the application to notify the user of the event without getting into way of current user task.

Showing text notifications:
Android has provided a class as Toast that can be used to show simple text messages. The Toast class provides static makeToast method to create the toast object. The text message can be shown as follows:

Toast.makeToast(context, “Some message text”, Toast.LENGTH_LONG);

The LENGTH_LONG is the duration of the notification. Another value for the duration is Toast.LENGTH_SHORT.

Notification with user defined views:
The Toast can also be used with user defined views. Let’s create a notification with some image. Here is the xml layout file for the Toast view:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/toast_frame">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/user_icon"
/>

<TextView
android:id="@+id/toast_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>
</FrameLayout>


The frame layout is using android defined drawable image toast_frame. The image view is using some image as user_icon (you have to add to user_icon.jpg image in res/drawable folder). Now we can use this layout for out toast.

		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.toast_view, null);
		// set the text in the view
		TextView message = (TextView) view.findViewById(R.id.toast_message);
		message.setText("Some test message");

		Toast toast = new Toast(this);
		toast.setView(view);
		toast.show();

The inflaterService is used to access the layout file. The toast will be shown with image and the message. You can create the view in the code also like this:

		TextView message = new TextView(this);
		message.setText("Some message");

		Toast toast = new Toast(this);
		toast.setView(message);
		toast.show();

Status Bar Notifications:
The notification can be shown in the phone status bar too. Android has provided Notification class that can be used to create the notification which can be shown on the status bar. To show notification NotificationManager is required. The NotificationManager used the android notification service to show the notifications. The content view and content intent has to be set to the notification. ContextView gets shown in the extended notification bar. The content intent is the intent that gets executed when notification entry in expanded status bar is clicked. The content Intent is the pending intent. The contentView and ContentIntent can be set with the help of setLatestEventInfo() method. This is how it can be done:

	NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		Notification notification = new Notification(R.drawable.icon,
				"Notification text that will be shown on status bar.", System
						.currentTimeMillis());

		// The PendingIntent will launch activity if the user selects this
		// notification
		PendingIntent contentIntent = PendingIntent.getActivity(context,
				REQUEST_CODE, new Intent(this, MyActivity.class), 0);
		notification.setLatestEventInfo(this, "Content Title", "Content text",
				contentIntent);
		manger.notify(NOTIFICATION_ID, notification);

Here pending intent is created for the activity. If the intent is executed the given activity will be shown. The notification can be cancelled with the help of notification id.

manger.cancel(NOTIFICATION_ID);

To see the expanded status bar go to emulator Home. Click on Menu, click Notification menu item. The expanded status bar will be shown with all the notifications. The values we set in contentIntent will be shown in the Notification entry.

Adding sound and Vibration to Notification:
The notification class provides various public fields. Out of which sound and vibrate can be used to set the notification sound and vibration pattern required for the notification. Here is the code:

notification.sound = "file:///sdcard/audiofile.mp3"
notification.vibrate = new long[] { 200, 300 };

The notification will vibrate for 300ms after 200ms of delay. Notification class also has default values defined for sound and vibrations those can be used as

notification.defaults = notification.DEFAULT_SOUND | notification.DEFAULT_VIBRATE;

Note for using vibrate functionality you need to define user permission for vibration in the AndroidManifest.xml file
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

One time or repeating sound and vibration:
You can also make the notification sound repeating or one time. Notification class has provided various flags for these settings.
Eg. To show the repeating notifications use

notification.flags = FLAG_INSISTENT;

One time notification is provided by default. Flag for one time notification is FLAG_ONLY_ALERT_ONCE.

Now you know the details as how to use notifications in android. Use notifications when application is running in background.

Android Basics , , , , , ,