3

I want to highlight a ListItem when notification is clicked. My activity which displays ListView is already open so I cannot open it again on clicking notification. I have searched a lot but I think there isn't any onClick() method for notification. So please tell how can I do this?

This is how I am generating notification

Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, notification_id, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setTicker("Smart Locator");
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle(name);
DetailsContainer dc = new LocationDetails(context).getDetails(location);
mBuilder.setContentText(date + ", " + dc.area + " " + dc.locality);
mBuilder.setContentIntent(pIntent).getNotification();
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(document_id, notification_id, mBuilder.build());
Raees Khan
  • 371
  • 1
  • 5
  • 20
  • I think clicking a notification creates an Intent, which you can specify in the manifest which activity can handle it. From there, you would need some data to know the type of notification you clicked and the position of the ListView data you wanted to select – OneCricketeer May 21 '16 at 14:34
  • Possible duplicate of [How to set click listener for notification?](http://stackoverflow.com/questions/7184963/how-to-set-click-listener-for-notification) – OneCricketeer May 21 '16 at 14:37

2 Answers2

3

You can put an extra in your pending intent.

final Intent intent = new Intent(this, MyActivity.class);
intent.setData(data);
intent.putExtra("key", "clicked");
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent), 0);

Then, when your activity 'restarts' you can check if the notification was clicked, via your bundle received.

@Override
protected void onCreate(Bundle savedInstanceState) {
    processIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {     
    processIntent(intent);
};

private void processIntent(Intent intent){
    //get your extras
    //if clicked, do something with ListView
}

Check this question for more.

Community
  • 1
  • 1
John Roberts
  • 467
  • 3
  • 5
1

Put an extra in your pending intent:

final Intent intent = new Intent(this, MyActivity.class);
intent.setData(data);
intent.putExtra("NotClick", true);
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent), 0);

And use the following code in your activity:

@Override
protected void onNewIntent(Intent intent) {
    try {
        //on click notification
        Bundle extras = intent.getExtras();
        if (extras.getBoolean("NotClick")) {
             //Do your stuff here 
        }
    } catch (Exception e) {
        Log.e("onclick", "Exception onclick" + e);
    }
}
mehdi
  • 340
  • 4
  • 17