Notifications

How to create the ever-essential Notification. You can also add Notification Actions. Notifications are also intrinsic to the concept of Foreground Services e.g. a music app that persists controls in the notification shade.

Notification channels are a means of categorising your notifications so that users have fine grained control over which notifications they receive and which they deactivate (rather than having to decide all or nothing). An example would be to have separate notification channels for essential information and promotional offers:

A Notification can be set up as follows:

public static void appNotification (Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // Get the NotificationManager system service
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // For Oreo and above we create a notification channel
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Awesome App", NotificationManager.IMPORTANCE_HIGH); // Set a unique ID for the channel, give it a name that will be displayed in the notification. and set the importance
            notificationManager.createNotificationChannel(notificationChannel); // Get the NotificationManager to create the channel
        }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) // NotificationCompat provides functionality to replace deprecated Notification methods and maintain backwards compatibility
                .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) // Sets the background colour of the notification. ContextCompat provides functionality to replace deprecated Context methods and maintain backwards compatibility
                .setSmallIcon(R.drawable.ic_app_smallIcon) // Sets the small icon drawable which will appear in the status bar
                .setLargeIcon(aBitmap) // Sets the large icon bitmap which will appear in the notification content view
                .setContentTitle(context.getString(R.string.notification_title)) // Sets the title
                .setContentText(context.getString(R.string.notification_body)) // Sets the text
                .setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.notification_body))) // Sets the style of the text
                .setDefaults(Notification.DEFAULT_VIBRATE) // Set the defaults - Notification.DEFAULT_SOUND, Notification.DEFAULT_VIBRATE, Notification.DEFAULT_LIGHTS. Or for all default values, use Notification.DEFAULT_ALL
                .setContentIntent(makePendingIntent(context)) // Feed in the PendingIntent (see method below)
                .setAutoCancel(true); // Notification automatically disappears once clicked
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { // For some build versions the priority needs to be set
            builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        notificationManager.notify(NOTIFICATION_ID, builder.build()); // Trigger the notification
    }
    private static PendingIntent makePendingIntent (Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        return PendingIntent.getActivity(context, PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

ud851-Exercises-student\Lesson10-Hydration-Reminder\T10.02