Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling buttons inside android notifications

I added a button inside a notification

but I don't know how to have it call a function when it's clicked.

I tried an approach like this https://code.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java since it's also using a RemoteViews object but nothing happens when I click the button.

This is what I currently have:

private void createNotification(){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

    Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_switch);

    //the intent that is started when the notification is clicked (works)
    Intent notificationIntent = new Intent(this, SettingsActivity.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;
    notification.flags |= Notification.FLAG_NO_CLEAR;

    //this is the intent that is supposed to be called when the button is clicked
    Intent switchIntent = new Intent(this, switchButtonListener.class);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);

    notificationView.setOnClickPendingIntent(R.id.buttonswitch, pendingSwitchIntent);

    notificationManager.notify(1, notification);
}

public static class switchButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "test");
    }

}

I can start an activity with the button but I didn't succeed to have it call a simple function. What would be the best way to do this?

Edit: I found out that I had to register "switchButtonListener" in AndroidManifest.xml

<receiver android:name="SettingsActivity$switchButtonListener" />

Source: Android Activity with no GUI

It works now.

like image 210
A E Avatar asked Sep 15 '12 14:09

A E


People also ask

How do I add buttons to my notifications?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to src/MainActivity.

How do you add action buttons to push notifications?

All you need to do is go to "New Message", select "Options", and add custom action buttons in the dashboard interface specific to which platform (mobile vs web) you are sending the notifications to. Each button will have its own identifier, call-to-action text, and if supported, an icon.

Which API do you use to add an action button to a notification?

1. API. Mobile - See REST API Create Notification: Action Buttons. The button id is added to the additionalData variable in the notification opened callback.


1 Answers

I found out that I had to register "switchButtonListener" in AndroidManifest.xml

<receiver android:name="SettingsActivity$switchButtonListener" />

Source: Android Activity with no GUI


Later I found out that I can also use code like this to achieve the same thing without modifying the manifest.

switchButtonListener = new SwitchButtonListener();
registerReceiver(switchButtonListener, new IntentFilter(SWITCH_EVENT));

.

public class switchButtonListener extends BroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "test");
    }

}

.

Intent switchIntent = new Intent(LangService.SWITCH_EVENT);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, switchIntent, 0);

notificationView.setOnClickPendingIntent(R.id.buttonswitch, pendingSwitchIntent);


Note that this way I can declare the switchButtonListener class without the static attribute (if not static, it would crash in the previous example) giving me much more flexibility.
Don't forget to call unregisterReceiver() later.

like image 164
A E Avatar answered Oct 26 '22 08:10

A E