0

In my application I have a notification to show.

Let's say when notification is show I want press 'Yes' to go an Activity and hide notification, press 'No' do nothing just hide notification.

I tried this code but instead of onclick is onClckPendingIntent and I can't do anything that I want.

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_push_layout);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContent(remoteViews)
                .setAutoCancel(true);

        Intent intent = new Intent(this,GPSTrackingActivity.class);
        final Intent yesIntent = new Intent(intent);
        final Intent noIntent = new Intent(this, GPSTrackingActivity.class);

        TaskStackBuilder yesStackBuilder = TaskStackBuilder.create(this);
        yesStackBuilder.addParentStack(MainActivity.class);
        yesStackBuilder.addNextIntent(yesIntent);

        TaskStackBuilder noStackBuilder = TaskStackBuilder.create(this);
        noStackBuilder.addParentStack(MainActivity.class);
        noStackBuilder.addNextIntent(noIntent);

        PendingIntent yesPendingIntent = yesStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent noPendingIntent = noStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


        remoteViews.setOnClickPendingIntent(R.id.btn_yes, yesPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.btn_no, noPendingIntent);
        mNotificationManager.notify(100, mBuilder.build());

How could I do this ?

pmb
  • 2,327
  • 3
  • 30
  • 47
  • Please check: [1]: http://stackoverflow.com/questions/21925688/adding-button-action-in-custom-notification [2]: http://stackoverflow.com/questions/19092559/perform-action-on-button-click-in-custom-notification-android [3]: http://stackoverflow.com/questions/29740430/custom-notifica – Joanmi Bardera Aug 16 '15 at 20:04
  • Thank you for your answer. I checked. but all examples out there opening some Activities as far as I understand – pmb Aug 16 '15 at 20:16

1 Answers1

0

I know it's kinda late, but I managed to get things done via receiver. you should:

  1. create a receiver
  2. create two intents to the receiver -

    String SHOULD_OPEN = "should_open_intent" Intent yes = new Intent(this, MyReceiver.class); yes.putBooleanExtra(SHOULD_OPEN, true); and same on Intent "no"

  3. wrap them with PendingIntent
  4. in you receiver, at the function "onReceive" get that data out using intent.getBooleanExtra(SHOUOLD_OPEN, DEFAULT_VALUE_DOESNT_MATTER) and handle it accordingly

check this out - https://stackoverflow.com/a/26486425/3339597

Community
  • 1
  • 1
Re'em
  • 1,869
  • 1
  • 22
  • 28