2

This is my notification, i need event when clicking button on notification. setOnClickPendingIntent doesn't work for me. R.layout.mynotification is under the code. (I don't want addAction).

        Intent intent = new Intent(Fragmentz.ctx, NotificationReceiverActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(Fragmentz.ctx, (int) System.currentTimeMillis(), intent, 0);

        // Build notification
        RemoteViews notificationView = new RemoteViews(Fragmentz.ctx.getPackageName(),
                R.layout.mynotification);

        Notification noti = new Notification.Builder(Fragmentz.ctx)
                .setContentTitle("Radio")
                .setContentText("").setSmallIcon(R.drawable.logo_notif)
                .setContentIntent(pIntent)
                .build();


        noti.contentView = notificationView;
        NotificationManager notificationManager = (NotificationManager) Fragmentz.ctx.getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

R.layout.mynotification.xml

<?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"
    android:weightSum="100" >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:text="Play"
        android:id="@+id/not_button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:src="@drawable/img_btn_play"
        android:background="@null"
        />


    </LinearLayout>
Nayra Ahmed
  • 657
  • 1
  • 9
  • 29

4 Answers4

0

You should add an intent through pendingIntent like so:

Intent intent = new Intent(context, desiredClass.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(pendingIntent);

Notice the flag FLAG_UPDATE_CURRENT and check if it is the right one for your case.

Amir Horev
  • 287
  • 2
  • 3
0

This is working for me:

    // Creates an explicit intent for an ResultActivity to receive.
    Intent resultIntent =  new Intent(Fragmentz.ctx, NotificationReceiverActivity.class);

    // This ensures that the back button follows the recommended convention for the back key.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
0

This should work for you

Add this line

Intent listener = new Intent(ctx, NotificationClickHandler.class);
PendingIntent pListener = PendingIntent.getActivity(ctx, 0, listener, 0);
notificationView.setOnClickPendingIntent(R.id.not_button, pListener);

And this in your manifest

<receiver android:name="com.xxx.NotificationClickHandler" /> // Use your package name

Finally create a Class NotificationClickHandler

public class NotificationClickHandler extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO: do your desired work here
    }   
}

Note: Rather than Activity this can be done using BroadcastReceiver too.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

What about this

      RemoteViews contentView = new RemoteViews(c
            .getPackageName(), R.layout.notification_custom);
 contentView.setTextViewText(R.id.buttonNotificationAccept, "Ok");
  contentView.setOnClickPendingIntent(R.id.buttonNotificationAccept,
                resultPendingIntent);
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110