title: Handling buttons inside android notifications
body: <p>I added a button inside a notification </p>
  
  <p><img src="https://i.stack.imgur.com/iTBWe.png" alt=""></p>
  
  <p>but I don't know how to have it call a function when it's clicked.</p>
  
  <p>I tried an approach like this <a href="https://code.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java" rel="nofollow noreferrer">https://code.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java</a> since it's also using a RemoteViews object but nothing happens when I click the button.</p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>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");
      }
  
  }
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>Edit:</strong>
  I found out that I had to register "switchButtonListener" in AndroidManifest.xml</p>
  
  <pre><code>&lt;receiver android:name="SettingsActivity$switchButtonListener" /&gt;
  </code></pre>
  
  <p>Source: <a href="https://stackoverflow.com/questions/526074/android-activity-with-no-gui">Android Activity with no GUI</a></p>
  
  <p>It works now.</p>