Android Basics – Learn how to use Android Notification Service

In order to send Notification to the user you can use the following steps, which have been explained in the video.

Create a Pending Intent

Intent intent = new Intent(this, MainActivity.class);

TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);

PendingIntent pendingIntent = taskStackBuilder.
        getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

 

Create a Notification object

Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(getString(R.string.app_name))
        .setContentText("Sample Notification")
        .setAutoCancel(true)
        .setPriority(Notification.PRIORITY_MAX)
        .setDefaults(Notification.DEFAULT_VIBRATE)
        .setContentIntent(pendingIntent)
        .build();

Use NotificationManager to send the Notification

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);

Source Code available here