Android notifications & implementation

  • Published on
    21-Jan-2018

  • View
    45

  • Download
    0

Transcript

1. Android Notifications & Implementation By Srinivas Zoftino.com 2. 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. 3. 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. 4. 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. 5. Notifications Android notification frame work provides a builder class NotificationCompat.Builder to create Notification objects. You can set flags using NotificationCompat.Builder methods and build Notification objects containing your flags for notification 6. 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. 7. 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. 8. 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. 9. 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. 10. 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. 11. 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()); 12. Thank You Visit http://www.zoftino.com/ for more info.