Custom Notification with remoteview in Android

Custom Notification

In the previous tutorial, we have discuss about notification . In this tutorial, I gonna to share how to make a custom notification with own layout. Where you can add button, progress bar and  any controls what you want.

On the click of notification controls, we can define action and for that we have to create pending intent. pending intent handle different types of action.

If you want to open an activity you have to use pending intent like this:
PendingIntent.getActivity() - Retrieve a Pending Intent to start an Activity

If you want to start a broadcast receiver you have to use pending intent like this:
PendingIntent.getActivity()- Retrieve a pending Intent  to perform a Broadcast

If you want to start a service you have to use pending intent like this:
PendingIntent.getService()- Retrieve a PendingIntent to start a service.


we have use Broadcast Receiver to get action when notification click.



public static class NotificationBroadcast extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NOTIFY_PLAY)) {
Toast.makeText(context,"Play Clicked",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(NOTIFY_PAUSE)) {
Toast.makeText(context,"Pause Clicked",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(NOTIFY_NEXT)) {
Toast.makeText(context,"Next Clicked",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(NOTIFY_DELETE)) {
NotificationManager mNotificationManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID);
}else if (intent.getAction().equals(NOTIFY_PREVIOUS)) {
Toast.makeText(context,"Previous Clicked",Toast.LENGTH_LONG).show();
}
}
public String ComponentName() {
return this.getClass().getName();
}
}

 register the broadcast receiver in manifest file


<receiver android:name="com.androidheight.customnotification.MainActivity$NotificationBroadcast" > <intent-filter>
<action android:name="com.androidheight.customnotification.delete" />
<action android:name="com.androidheight.customnotification.pause" />
<action android:name="com.androidheight.customnotification.next" />
<action android:name="com.androidheight.customnotification.play" />
<action android:name="com.androidheight.customnotification.previous" />
</intent-filter>
</receiver>





activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidheight.customnotification.MainActivity">
<Button
android:id="@+id/btnNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to Show custom Notification"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

custom_notification_layout.xml



<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageViewAlbumArt"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/music" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/textSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:selectAllOnFocus="true"
android:singleLine="true"
android:text="Song Name"
android:textColor="@android:color/darker_gray"
android:textSize="20dp" />
<TextView
android:id="@+id/textAlbumName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:selectAllOnFocus="true"
android:text="Album Name"
android:singleLine="true"
android:textColor="@android:color/darker_gray"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btnPrevious"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:background="@drawable/rewind" />
<Button
android:id="@+id/btnPause"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/pause" />
<Button
android:id="@+id/btnPlay"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/play"
android:visibility="gone" />
<Button
android:id="@+id/btnNext"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/fast_frw" />
<Button
android:id="@+id/btnDelete"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:background="@drawable/cross" />
</LinearLayout>
</LinearLayout>
</LinearLayout> big_custom_notification.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageViewAlbumArt"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/music" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Song Name"
android:singleLine="true"
android:textColor="@android:color/darker_gray"
android:textSize="20dp"/>
</LinearLayout>
<TextView
android:id="@+id/textAlbumName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Album Name"
android:singleLine="true"
android:textColor="@android:color/darker_gray"
android:textSize="15dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:dividerPadding="12.0dip"
android:gravity="center"
android:orientation="horizontal"
android:showDividers="middle" >
<Button
android:id="@+id/btnPrevious"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/rewind" />
<Button
android:id="@+id/btnPause"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/pause" />
<Button
android:id="@+id/btnPlay"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/play"
android:visibility="gone" />
<Button
android:id="@+id/btnNext"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/fast_frw"/>
<Button
android:id="@+id/btnDelete"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:background="@drawable/cross" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout> MainActivity.java

package com.androidheight.customnotification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btnNotification;
boolean currentVersionSupportBigNotification = currentVersionSupportBigNotification();
boolean SONG_PAUSED =false;
static int NOTIFICATION_ID = 100;
public static final String NOTIFY_PREVIOUS = "com.androidheight.customnotification.previous";
public static final String NOTIFY_DELETE = "com.androidheight.customnotification.delete";
public static final String NOTIFY_PAUSE = "com.androidheight.customnotification.pause";
public static final String NOTIFY_PLAY = "com.androidheight.customnotification.play";
public static final String NOTIFY_NEXT = "com.androidheight.customnotification.next";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnNotification).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newNotification();
}
});
}
private void newNotification() {
String songName = "Song Name";
String albumName = "Album Name";
RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),R.layout.custom_notification_layout);
RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.big_custom_notification);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(songName).build();
setListeners(simpleContentView);
setListeners(expandedView);
notification.contentView = simpleContentView;
if(currentVersionSupportBigNotification){
notification.bigContentView = expandedView;
}
try{
notification.contentView.setImageViewResource(R.id.imageViewAlbumArt, R.drawable.music);
if(currentVersionSupportBigNotification){
notification.bigContentView.setImageViewResource(R.id.imageViewAlbumArt, R.drawable.music);
}
}
catch(Exception e){
e.printStackTrace();
}
if(SONG_PAUSED){
notification.contentView.setViewVisibility(R.id.btnPause, View.GONE);
notification.contentView.setViewVisibility(R.id.btnPlay, View.VISIBLE);
if(currentVersionSupportBigNotification){
notification.bigContentView.setViewVisibility(R.id.btnPause, View.GONE);
notification.bigContentView.setViewVisibility(R.id.btnPlay, View.VISIBLE);
}
SONG_PAUSED=false;
}else{
notification.contentView.setViewVisibility(R.id.btnPause, View.VISIBLE);
notification.contentView.setViewVisibility(R.id.btnPlay, View.GONE);
if(currentVersionSupportBigNotification){
notification.bigContentView.setViewVisibility(R.id.btnPause, View.VISIBLE);
notification.bigContentView.setViewVisibility(R.id.btnPlay, View.GONE);
}
SONG_PAUSED=true;
}
notification.contentView.setTextViewText(R.id.textSongName, songName);
notification.contentView.setTextViewText(R.id.textAlbumName, albumName);
if(currentVersionSupportBigNotification){
notification.bigContentView.setTextViewText(R.id.textSongName, songName);
notification.bigContentView.setTextViewText(R.id.textAlbumName, albumName);
}
notification.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
/**
* Notification click listeners
* @param view
*/
public void setListeners(RemoteViews view) {
Intent previous = new Intent(NOTIFY_PREVIOUS);
Intent delete = new Intent(NOTIFY_DELETE);
Intent pause = new Intent(NOTIFY_PAUSE);
Intent next = new Intent(NOTIFY_NEXT);
Intent play = new Intent(NOTIFY_PLAY);
PendingIntent pPrevious = PendingIntent.getBroadcast(getApplicationContext(), 0, previous, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);
PendingIntent pDelete = PendingIntent.getBroadcast(getApplicationContext(), 0, delete, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnDelete, pDelete);
PendingIntent pPause = PendingIntent.getBroadcast(getApplicationContext(), 0, pause, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPause, pPause);
PendingIntent pNext = PendingIntent.getBroadcast(getApplicationContext(), 0, next, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnNext, pNext);
PendingIntent pPlay = PendingIntent.getBroadcast(getApplicationContext(), 0, play, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
}
public static boolean currentVersionSupportBigNotification() {
int sdkVersion = android.os.Build.VERSION.SDK_INT;
if(sdkVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN){
return true;
}
return false;
}
public static class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NOTIFY_PLAY)) {
Toast.makeText(context,"Play Clicked",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(NOTIFY_PAUSE)) {
Toast.makeText(context,"Pause Clicked",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(NOTIFY_NEXT)) {
Toast.makeText(context,"Next Clicked",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(NOTIFY_DELETE)) {
NotificationManager mNotificationManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID);
}else if (intent.getAction().equals(NOTIFY_PREVIOUS)) {
Toast.makeText(context,"Previous Clicked",Toast.LENGTH_LONG).show();
}
}
public String ComponentName() {
return this.getClass().getName();
}
}
}



       output

















You can find the complete project on GITHUB
SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment