title: How to trigger a broadcast receiver from notification?
body: <p>I want to trigger a Broadcast receiver from the notification. When I click on a button which is on the notification it shows this error:</p>
  <blockquote>
  <p>&quot;Unable to instantiate receiver com.example.testservice.myBroad:
  java.lang.ClassCastException: com.example.testservice.myBroad cannot
  be cast to android.content.BroadcastReceiver&quot;</p>
  </blockquote>
  <p>**updated/ edited and its working now **</p>
  <ol>
  <li><p>Can you please help to handle 2 buttons from notification to broadcast receiver?
  How can I pass extra value from notification broadcast trigger to a receiver that whether its play button pressed or pause?</p>
  </li>
  <li><p>Now my button working but when I click on notification text it does not take me into my activity. Any help?</p>
  </li>
  </ol>
  <p>I write this code for 2 buttons with extra on intent.</p>
  <pre><code> RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
              layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
              Intent clickIntent = new Intent();
              clickIntent.putExtra(&quot;button&quot;,&quot;pause&quot;);
              clickIntent.setAction(ACTION_DIALOG);
             
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, pendingFlag);
              layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
              builder.setContent(layout);
              
              
               layout = new RemoteViews(getPackageName(), R.layout.notification);
              layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
              Intent click = new Intent();
              clickIntent.putExtra(&quot;Button&quot;,&quot;play&quot;);
              clickIntent.setAction(ACTION_DIALOG);
             
              PendingIntent pi1 = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, click, pendingFlag);
              layout.setOnClickPendingIntent(R.id.notification_button1,pi1);
              builder.setContent(layout);
  </code></pre>
  <p>myBroad receiver file</p>
  <pre><code>Bundle extrasBundle = intent.getExtras();
      String str = (String) extrasBundle.get(&quot;button&quot;);       
      Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
  
      context.stopService(new Intent(context, myPlayService.class));
  </code></pre>
  <p>Here is my code:</p>
  <pre><code>void showNotification() {
       int pendingRequestCode = 0;
          int pendingFlag = 0;
  
          final Resources res = getResources();
          final NotificationManager notificationManager = (NotificationManager) getSystemService(
                  NOTIFICATION_SERVICE);
          Intent intent = new Intent(MainActivity.this,myBroad.class);
          PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
          Notification.Builder builder = new Notification.Builder(this)
                  .setSmallIcon(R.drawable.ic_action_search)
                  .setAutoCancel(true)
                  .setTicker(&quot;this is notification&quot;)
                  .setContentIntent(getDialogPendingIntent(&quot;Tapped the notification entry.&quot;));
  
          
              // Sets a custom content view for the notification, including an image button.
              RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
              layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
              Intent clickIntent = new Intent();
              clickIntent.setAction(ACTION_DIALOG);
             
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, pendingFlag);
              layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
              builder.setContent(layout);
  
              // Notifications in Android 3.0 now have a standard mechanism for displaying large
              // bitmaps such as contact avatars. Here, we load an example image and resize it to the
              // appropriate size for large bitmaps in notifications.
              Bitmap largeIconTemp = BitmapFactory.decodeResource(res,
                      R.drawable.notification_default_largeicon);
              Bitmap largeIcon = Bitmap.createScaledBitmap(
                      largeIconTemp,
                      res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                      res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
                      false);
              largeIconTemp.recycle();
  
              builder.setLargeIcon(largeIcon);
  
          notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
   }
      
  
      PendingIntent getDialogPendingIntent(String dialogText) {
          return PendingIntent.getActivity(
                  this,
                  dialogText.hashCode(), // Otherwise previous PendingIntents with the same
                                         // requestCode may be overwritten.
                  new Intent(ACTION_DIALOG)
                          .putExtra(Intent.EXTRA_TEXT, dialogText)
                          .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                  0);
      }
   
  </code></pre>
  <p>myBroad.class</p>
  <pre><code>public class myBroad extends BroadcastReceiver{
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          Toast.makeText(context, &quot;received&quot;, Toast.LENGTH_SHORT).show();
      
          context.stopService(new Intent(context, myPlayService.class));  
      }
  }
  </code></pre>
  <p>Manifest file is:</p>
  <pre><code> &lt;application
      android:icon=&quot;@drawable/ic_launcher&quot;
      android:label=&quot;@string/app_name&quot;
      android:theme=&quot;@style/AppTheme&quot; &gt;
      &lt;activity
          android:name=&quot;.MainActivity&quot;
          android:label=&quot;@string/title_activity_main&quot; &gt;
          &lt;intent-filter&gt;
              &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
             
  
              &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
          &lt;/intent-filter&gt;
            &lt;/activity&gt;
            
       &lt;receiver android:name=&quot;.myBroad&quot;&gt;
          &lt;intent-filter &gt;
  &lt;action android:name=&quot;android.intent.action.MEDIA_BUTTON&quot;/&gt;
      &lt;/intent-filter&gt;
  
          &lt;/receiver&gt;
    
      &lt;service android:name=&quot;com.example.testservice.myPlayService&quot; android:icon=&quot;@drawable/ic_action_search&quot; android:label=&quot;@string/app_name&quot; android:enabled=&quot;true&quot;/&gt;
      
      
  &lt;/application&gt;
  </code></pre>