Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Notifications

What are Notifications?

Notifications in Android are messages that are displayed to the user outside of the app's normal UI. They provide a way for your application to notify users about events, such as a new message, an upcoming event, or a status update.

Why Use Notifications?

Notifications are a key component in keeping users engaged with your app. They can inform users of important timely information, encourage them to open the app, or prompt them to take action directly from the notification itself.

Creating a Simple Notification

To create a notification in Android, you typically use the NotificationCompat.Builder class to set the various properties of the notification and then issue the notification using the NotificationManager service.

Here is an example of creating and displaying a simple notification:


NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, builder.build());
                

Notification Channels

Starting with Android 8.0 (API level 26), all notifications must be assigned to a channel. Notification channels allow you to group notifications and manage their behavior in a unified way.

Here is an example of creating a notification channel:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = getString(R.string.channel_name);
    String description = getString(R.string.channel_description);
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}
                

Adding Actions to Notifications

Notifications can include actions that allow users to interact with them without opening the app. These actions can be added using the addAction() method of the NotificationCompat.Builder class.

Here is an example of adding an action to a notification:


Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(ACTION_SNOOZE);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
PendingIntent snoozePendingIntent =
        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
                

Conclusion

Notifications are a powerful tool in Android development, enabling you to keep users informed and engaged with your app. By understanding the basics of creating notifications, setting up notification channels, and adding actions, you can create a more interactive and responsive user experience.