A notification is a message or information you can display to the user outside of your Android Application. Notifications can be clicked to perform an action or to open a new activity to indicate that an event has occurred.
Custom Notification is a notification with our own designed UI, through which we can access all different default/custom controls and can interact with all the controls of notification. We can give unique feel to our application through custom notification.
There are two types of views of notification.
- Normal View.
- Big View.
Steps To Create Custom Notification
Step 1:
Create new project in Android Studio. Create three layouts. First layout with one button to create notification, second layout for displaying Normal View, third layout for displaying Big View as per our need.
Step 2:
Now after that, We need to use RemoteViews to create and deal with Custom Notification. RemoteViews is a view hierarchy which is displayed in another process.That is inflated from layout resource file, and it provides operations for change content of the inflated hierarchy. In simple terms RemoteViews is used to interact and bind custom layouts with widget and notification. And also used Broadcast to perform action of UI click.
How to set layout and how to access the controls is shown below.
private void setRemoteViews(RemoteViews remoteViews) { // set Intent to open app on notification click. Intent openAppIntent = new Intent(this, MainActivity.class); // call broadcast when any control of notification is clicked. Intent closeNotification = new Intent("close_notification"); Intent playPauseIntent = new Intent("play_pause"); Intent previousInt = new Intent("previous_intent"); Intent nextInt = new Intent("next_intent"); PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, closeNotification, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingPlayPauseIntent = PendingIntent.getBroadcast(this, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingPreviousIntent = PendingIntent.getBroadcast(this, 0, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingNextIntent = PendingIntent.getBroadcast(this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingOpenIntent = PendingIntent.getActivity(this, 0, openAppIntent, PendingIntent.FLAG_UPDATE_CURRENT); // Using RemoteViews to bind custom layouts into Notification remoteViews.setOnClickPendingIntent(R.id.notify_player_control_iv_cancel, pendingCloseIntent); remoteViews.setOnClickPendingIntent(R.id.custom_notificaiton_rl, pendingOpenIntent); remoteViews.setOnClickPendingIntent(R.id.notify_player_control_iv_pause, pendingPlayPauseIntent); remoteViews.setOnClickPendingIntent(R.id.notify_player_control_iv_next, pendingNextIntent); remoteViews.setOnClickPendingIntent(R.id.notify_player_control_iv_previous, pendingPreviousIntent); }
BroadcastReceiver to perform action. We need to register this broadcast in application’s manifest and unregister it when activity is destroyed.
private BroadcastReceiver mDeleteReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifManager.cancel(100); } }; private BroadcastReceiver mPreviousReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(MainActivity.this, "Previous Song", Toast.LENGTH_SHORT).show(); } }; private BroadcastReceiver mPlayPauseReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(MainActivity.this, "Play-Pause Song", Toast.LENGTH_LONG).show(); } }; private BroadcastReceiver mNextReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(MainActivity.this, "Next Song", Toast.LENGTH_LONG).show(); } };
Step 3:
Method to Create Notification.
Normal View is supported in all the version but to take the advantage of Big View of notification the SDK version of device should be jelly_bean or above that. Using NotificationCompat.Builder we will set notification. WE need to set Intent into PendingIntent which is passed as an argument in Notification object.
private NotificationCompat.Builder mBuilder; private static boolean supportBigNotifications = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; private static boolean supportSmallNotifications = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; private Notification notifyPlayer; private RemoteViews remoteViews; private void showCustomNotification() { RemoteViews mContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.your_custom_notification); Intent intent = new Intent(getApplicationContext(), MainActivity.class); if (supportBigNotifications) { remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_big_notification); } PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); // Create Notification. mBuilder = new NotificationCompat.Builder(getApplicationContext()); notifyPlayer = mBuilder.setSmallIcon(R.drawable.movie_image) .setContentIntent(pendingIntent) .setOngoing(true) .build(); // Assign remoteview to notification if (supportSmallNotifications) { notifyPlayer.contentView = mContentView; if (supportBigNotifications) { notifyPlayer.bigContentView = remoteViews; } } else { if (supportBigNotifications) { notifyPlayer.bigContentView = remoteViews; } } // manage clicks of notification setRemoteViews(mContentView); if (supportBigNotifications) { setRemoteViews(remoteViews); } NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(100, notifyPlayer); }
Step 4:
Now final step to view notification in notification bar. Call showCustomNotification() on button click. When user will click this button, you will find one notification on your notification bar with different buttons.
findViewById(R.id.btn_show_notify).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showCustomNotification(); } });
This was the simple and easy way to create notification with custom layout without any user permission.