Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to differentiate between on notification click and on notification button click

when I'm clicking the notification it self and the action button it does the same, I want to differentiate between them, I want the button do something and when I click the notification is self something else, here is my code-

    Intent intent = new Intent(this, NotificationView.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker(getString(R.string.notificationticker))
            .setContentTitle(getString(R.string.notificationtitle))
            .setContentText(yeah)
            .addAction(R.drawable.ic_favorite, getString(R.string.azor), pIntent)
            .setContentIntent(pIntent)
            .setAutoCancel(true);

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationmanager.notify(0, builder.build());
like image 624
Tsur Yohananov Avatar asked Dec 05 '25 05:12

Tsur Yohananov


1 Answers

Pass different PendingIntents to addAction and setContentIntent.
Currently they both use the same pIntent instance.

If you want them both to launch the same activity but do something different, you can add extra's to the Intent used to make the pending intent.

like image 131
RobCo Avatar answered Dec 06 '25 21:12

RobCo