Event Onclick for a Button in a Custom Notification

Event OnClick for a button in a custom notification

I am writing code in my MyActivity.java class that extends android.app.Activity

It creates a custom notification, when user click on the button it sends a broadcast.
There is a broadcast receiver that receives the broadcast.

private void createDownloadNotification() {
Intent closeButton = new Intent("Download_Cancelled");
closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, closeButton, 0);

RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.widget_update_notification);

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

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setTicker("Ticker Text").setContent(notificationView);
notificationView.setProgressBar(R.id.pb_progress, 100, 12, false);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);

notificationManager.notify(1, builder.build());

}

public static class DownloadCancelReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

System.out.println("Received Cancelled Event");
}
}

Register receiver in AndroidManifest.xml

<receiver android:name=".MainActivity$DownloadCancelReceiver" android:exported="false">
<intent-filter>
<action android:name="Download_Cancelled" />
</intent-filter>
</receiver>

Since it is inner class so have to use $ sign

Widget xml is here

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >

<Button
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close Me" />

<ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Android custom notification button onClick not working

Registered the broadcast receiver at runtime and now it works fine :-)

  private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

assert action != null;
switch (action) {
case Intent.ACTION_HEADSET_PLUG:
refreshDSP();
sendCoreBroadcast(getString(R.string._coreEventReloadDSP_UI));
break;
case "android.media.AUDIO_BECOMING_NOISY":
if (prefs.getBoolean(context.getResources().getString(R.string._prefs_key_headphoneCanPause), true)) {
pause();
AMPlayerCore.isPaused = true;
}
break;

case "com.hbs.andMovie.notification.TOGGLE":
toggle();
break;
case "com.hbs.andMovie.notification.FF":
if (!playNext()) {
sendCoreBroadcast(getString(R.string._broadcastActionFailedToChangeMedia));
}
break;
case "com.hbs.andMovie.notification.REW":
if (!playPrevious()) {
sendCoreBroadcast(getString(R.string._broadcastActionFailedToChangeMedia));
}
break;
case "com.hbs.andMovie.notification.CLOSE":
close_notification();
break;

default:
// update(context);
break;
}

}

};

IntentFilter ifilter = new IntentFilter();
ifilter.addAction("com.hbs.andMovie.notification.TOGGLE");
ifilter.addAction("com.hbs.andMovie.notification.CLOSE");
ifilter.addAction("com.hbs.andMovie.notification.FF");
ifilter.addAction("com.hbs.andMovie.notification.REW");
ifilter.addAction("android.media.AUDIO_BECOMING_NOISY");
ifilter.addAction(Intent.ACTION_HEADSET_PLUG);
registerReceiver(mReceiver, ifilter);

adding on click event to button in notification

I think you want to add a custom button in your notification and want to click it.

Please try below code if you need the same:

You have to use RemoteViews for this.

I have created one custom layout named notification_normal_view.xml.

In my notification_normal_view , I have one TextView i.e.txtSnooze and on click I want to open SnoozeActivity and if I click at any other part of notification I want to open MainActivity.

So in your receiver :

// Using RemoteViews to bind custom layouts into Notification
RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_normal_view);

Intent snoozeIntent = new Intent(context, SnoozeActivity.class);
snoozeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);

PendingIntent pSnoozeIntent = PendingIntent.getBroadcast(context,NOTIFICATION_ID,snoozeIntent,PendingIntent.FLAG_UPDATE_CURRENT);

Intent intent = new Intent(context, ExoVideoPlayer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);
PendingIntent pIntent = PendingIntent.getActivity(context, NOTIFICATION_ID,intent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationView.setOnClickPendingIntent(R.id.txt_snooze, pSnoozeIntent);

Notification notificationBuilder = new Notification.Builder(context)
.setSound(soundUri)
.setSmallIcon(icon)
.setAutoCancel(true)
.build();

//set your view to notification
notificationBuilder.contentView = notificationView;
notificationBuilder.flags = Notification.FLAG_AUTO_CANCEL;
notificationBuilder.icon = R.mipmap.ic_launcher;
notificationBuilder.contentIntent = pIntent;

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

mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder);


Related Topics



Leave a reply



Submit