Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Listener when a notification is clicked or dismissed in Android

I want to know is there a way for the NotificationListenerService to know if a Notification has been clicked or it has been dismissed. I can see that the Inner class NotificationListenerWrapper in NotificationListenerService has a onNotificationClick() method , but since the NotificationListenerWrapper is hidden with @hide annotation I'm not able to use that.

My question is Can I write a Listener which basically keeps track of whether a notification has been clicked or dismissed.

Basically I want to track if the notifications of my App is being dismissed or they are clicked without any intrusive code in each and every Notification.

P.S. NotificationListenerService provides only onNotificationPosted() and onNotificationRemoved(), but my requirement is to know if notifications are clicked or Removed.

Thanks

like image 905
Aneez Avatar asked Mar 01 '17 09:03

Aneez


2 Answers

When you create and show a notification, you provide a PendingIntent for when it is clicked (using .setContentIntent). You can also define a PendingIntent for when it gets dismissed(using .setDeleteIntent(PendingIntent).

If you want to be able to track your Notifications, you need to pass a PendingIntent (like a BroadcastReceiver or IntentService) and pass a few parameters like notificationId and isDismissed and do your work in the BroadcastReceiver.

You can see the complete code in this answer.

like image 77
Adib Faramarzi Avatar answered Oct 26 '22 18:10

Adib Faramarzi


I don't think there is a listener for this. But you can implement this logic in another way using PendingIntent and BroadcastReceiver

For OnClick

add a ContentIntent and BroadcastReceiver. So you will know that your notification is clicked or not in the BroadcastReceiver

     Intent onClickIntent = new Intent(this, OnClickBroadcastReceiver.class);
     PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, onClickIntent, 0);
     mBuilder.setContentIntent(onClickPendingIntent);

And inside the BroadcastReceiver you can write your logic

 public class OnClickBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       //Open your activity here
        Intent mainIntent = new Intent(context, YourActivity.class);
        context.startActivity(mainIntent);

        // Do your onClick related logic here
    }

}

For onDismiss

For this you need to add a DeleteIntent into your notification builder

 Intent onCancelIntent = new Intent(this, OnCancelBroadcastReceiver.class);
            PendingIntent onDismissPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, onCancelIntent, 0);
            mBuilder.setDeleteIntent(onDismissPendingIntent);

and BroadcastReceiver for this is

    public class OnCancelBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //Do your logic here
    }

}

Don't forget to register these BroadcastReceiver to AndroidManifest

like image 25
Vinayak B Avatar answered Oct 26 '22 17:10

Vinayak B