Android Notifications & Implementation
By Srinivas
Zoftino.com
Android Notifications
Android notifications are displayed as icons in notification area. Users can view details of notification by opening notification drawer.
Notification feature allows apps to notify event or latest information to users when app is open or closed.
Android Notifications
Notifications can be simple text messages or complex with options for users to perform actions.
Notification should at least have small icon, content title and content text information.
Android Notifications
You need to define at least one action that needs to be performed when user clicks notification text
To handle the action events from notifications, you need to create activity class for each action.
Notifications
Android notification frame work provides a builder class NotificationCompat.Builder to create Notification objects. You can set flags using NotificationCompat.Buildermethods and build Notification objects containing your flags for notification
Notifications
NotificationCompat.Builder provides methods which allow you to set different types of actions for notifications using PendingIntent.
Using NotificationManager, you can send notification to system by calling notify on NotificationManager.
Best Practices
Create notifications with NotificationCompat.Builder to achieve compatibility.
Implement activities for all actions defined in notification.
Update previous notification by using notification id instead of creating new notification if both notifications are of same type.
Best Practices
You can clear outdated notifications by calling cancel passing notification id.
It is good practice to bundle similar notifications.
If user starts an activity by clicking actions on notification, you need to make sure that back navigation works properly.
Notifications Implementation
To send notifications when your app is closed, alarm can be used.
Use ACTION_BOOT_COMPLETED system broadcast to register an alarm which would frequently fire notification intents.
Alarm registration is a one time activity.
Notifications Implementation
The pending intent, which alarm sends, would start a service which intern sends notifications.
Notification are associated with pending intents which start appropriate activity in your app for further user interactions.
Notifications Implementation
Context context = getApplicationContext();
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.notification);
notificationBuilder.setContentTitle(“My app message");
notificationBuilder.setContentText(“hello , new info is available");
Intent notificationClickIntent = new Intent(this, NotificationActionActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(context, 0, notificationClickIntent, 0);
notificationBuilder.setContentIntent(notificationPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notificationBuilder.build());