Transcript
Page 1: Android notifications & implementation

Android Notifications & Implementation

By Srinivas

Zoftino.com

Page 2: Android notifications & implementation

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.

Page 3: Android notifications & implementation

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.

Page 4: Android notifications & implementation

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.

Page 5: Android notifications & implementation

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

Page 6: Android notifications & implementation

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.

Page 7: Android notifications & implementation

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.

Page 8: Android notifications & implementation

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.

Page 9: Android notifications & implementation

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.

Page 10: Android notifications & implementation

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.

Page 11: Android notifications & implementation

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());

Page 12: Android notifications & implementation

Thank You

Visit http://www.zoftino.com/ for more info.


Recommended