How to Update Notification with Remoteviews

How to update Notification with RemoteViews?

Here's a detail example for you to update the notification using RemoteViews:

private static final int NOTIF_ID = 1234;
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotificationManager;
private RemoteViews mRemoteViews;
private Notification mNotification;
...

// call this method to setup notification for the first time
private void setUpNotification(){

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// we need to build a basic notification first, then update it
Intent intentNotif = new Intent(this, MainActivity.class);
intentNotif.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intentNotif, PendingIntent.FLAG_UPDATE_CURRENT);

// notification's layout
mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small);
// notification's icon
mRemoteViews.setImageViewResource(R.id.notif_icon, R.drawable.ic_launcher);
// notification's title
mRemoteViews.setTextViewText(R.id.notif_title, getResources().getString(R.string.app_name));
// notification's content
mRemoteViews.setTextViewText(R.id.notif_content, getResources().getString(R.string.content_text));

mBuilder = new NotificationCompat.Builder(this);

CharSequence ticker = getResources().getString(R.string.ticker_text);
int apiVersion = Build.VERSION.SDK_INT;

if (apiVersion < VERSION_CODES.HONEYCOMB) {
mNotification = new Notification(R.drawable.ic_launcher, ticker, System.currentTimeMillis());
mNotification.contentView = mRemoteViews;
mNotification.contentIntent = pendIntent;

mNotification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
mNotification.defaults |= Notification.DEFAULT_LIGHTS;

// starting service with notification in foreground mode
startForeground(NOTIF_ID, mNotification);

}else if (apiVersion >= VERSION_CODES.HONEYCOMB) {
mBuilder.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendIntent)
.setContent(mRemoteViews)
.setTicker(ticker);

// starting service with notification in foreground mode
startForeground(NOTIF_ID, mBuilder.build());
}
}

// use this method to update the Notification's UI
private void updateNotification(){

int api = Build.VERSION.SDK_INT;
// update the icon
mRemoteViews.setImageViewResource(R.id.notif_icon, R.drawable.icon_off2);
// update the title
mRemoteViews.setTextViewText(R.id.notif_title, getResources().getString(R.string.new_title));
// update the content
mRemoteViews.setTextViewText(R.id.notif_content, getResources().getString(R.string.new_content_text));

// update the notification
if (api < VERSION_CODES.HONEYCOMB) {
mNotificationManager.notify(NOTIF_ID, mNotification);
}else if (api >= VERSION_CODES.HONEYCOMB) {
mNotificationManager.notify(NOTIF_ID, mBuilder.build());
}
}

Layout for the Notification, i.e. res/layout/custom_notification_small.xml:

<!-- We have to set the height to 64dp, this is the rule of the small notification -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal"
android:id="@+id/notif_small"
android:background="@drawable/notification_background">

<ImageView
android:id="@+id/notif_icon"
android:contentDescription="@string/notif_small_desc"
android:layout_width="47dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher"
android:layout_marginLeft="7dp"
android:layout_marginRight="9dp"/>

<TextView
android:id="@+id/notif_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notif_icon"
android:singleLine="true"
android:paddingTop="8dp"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="@string/app_name"/>

<TextView
android:id="@+id/notif_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notif_icon"
android:paddingBottom="9dp"
android:layout_alignParentBottom="true"
android:singleLine="true"
android:textSize="13sp"
android:textColor="#575757"
android:text="Content" />
</RelativeLayout>

Hope this example helps you a lot!

NOTE: You can't update the custom NotificationCompat on pre-Honeycomb, so I added an alternative way to update it on pre-Honeycomb, i.e. checking the API level first and use the deprecated Notification instead.

Custom Android notification with RemoteView and actions

I was in the same boat. I wanted a custom view on top, with the options of the action buttons on the bottom. What you're looking for is the NotificationCompat.DecoratedCustomViewStyle.

Here is an example

Android remoteviews in notification

I found out what was wrong.
In my case i was trying to show a layout which was greater than the default notification layout and as the result it was compressing my layout to fit the size of default notification.

I changed the views like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/album_art" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/wrapper"
android:descendantFocusability="blocksDescendants"
android:layout_toRightOf="@+id/album_art">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:gravity="center_vertical"
android:orientation="vertical"
android:id="@+id/info"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:textStyle="bold"
/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="singer"
/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:id="@+id/player"
android:layout_below="@+id/divider_horizontal"
android:layout_marginTop="10dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prev"
android:layout_marginRight="20dp"
android:src="@drawable/ic_fast_rewind"
android:layout_gravity="center_vertical" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#82c7c7c7"></LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/play"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_pause" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#82c7c7c7" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_fast_forward" />

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="@+id/divider_horizontal"
android:background="#82c7c7c7"
android:layout_below="@+id/info"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"></LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/close"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/ic_close" />

</RelativeLayout>

</RelativeLayout>

And in the notification method i added the following lines which solved the problem:

RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notification_player);

builder = new NotificationCompat.Builder(getApplicationContext())
.setContent(rv)
.setTicker("IranGrammy playing")
.setAutoCancel(true)
.setSmallIcon(ir.farzamhabibi.moozik.R.drawable.ic_launcher);

rv.setImageViewBitmap(R.id.album_art, aq.getCachedImage(item.getImage()));
rv.setTextViewText(R.id.title, item.getTitle());
rv.setTextViewText(R.id.text, item.getSinger());
rv.setOnClickPendingIntent(R.id.close, delete);
rv.setOnClickPendingIntent(R.id.play, playPI);
rv.setOnClickPendingIntent(R.id.next, nextPI);
rv.setOnClickPendingIntent(R.id.prev, prevPI);
Notification notif = builder.build();
notif.bigContentView = rv;
startForeground(10, notif);

Update the text of a button contained in a custom notification when clicked in Android

Update

 public void onReceive(final Context context, Intent intent) {

NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.cam,null,System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

String condition = intent.getStringExtra("alarm_state");

if (condition.compareToIgnoreCase("on")==0) //here it's always false!!! Why??
{
contentView.setImageViewResource(R.id.switch1,R.drawable.button_on);
contentView.setTextViewText(R.id.subtitle,"Cam enabled");
Intent button_intent=new Intent(context,ButtonListener.class);
button_intent.putExtra("alarm_state","off");
PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
}
if (condition.compareToIgnoreCase("off")==0)
{
contentView.setImageViewResource(R.id.switch1,R.drawable.button_off);
contentView.setTextViewText(R.id.subtitle,"Cam disabled");
Intent button_intent=new Intent(context,ButtonListener.class);
button_intent.putExtra("alarm_state","on");
PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
contentView.setOnClickPendingIntent(R.id.switch1,p_button_intent);
}

notify.contentView=contentView;
nm.notify(1,notify);
}

old

You can not change the title of your action button after creating the notification already. But what you can do, you can set a background drawable which will be different based on state. something like this -

Lets say you have a my_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_on_image" android:state_pressed="true"/>
<item android:drawable="@drawable/button_on_image" android:state_focused="true"/>
<item android:drawable="@drawable/button_off_image"/>
</selector>

then -

addAction(R.drawable.my_button, <title>, <intent>);

Or you can do what @Pawel said, but thats creating another new notification replacing the old one.



Related Topics



Leave a reply



Submit