Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when Notification is clicked to send analytic's event

We have an app with GCM notifications working fine, we set a PendingIntent so the activity that we want is open when the notification is clicked. However, we need to send an event to Google Analytics each time that one notification (and there are different types of notifications) is clicked. How could we achieve this? I don't want to parse the intent in the activity, as I think that this would not be a good solution (we are using TaskStackBuilder so not always the same activity is open for the same notification), is there a receiver we can use to detect when the notification is clicked/open?

Thanks in advance

like image 670
FVod Avatar asked Nov 15 '25 19:11

FVod


2 Answers

put some flag variable on your notification intent.

intent.putExtra("notification","clicked");

Now check it in your Activity, whether Bundle is having "notification" key or not

if(getIntent().hasExtra("notification"))
    //write your code for analytics
like image 181
Ravi Avatar answered Nov 17 '25 09:11

Ravi


Try using NotificationListenerService. It allows an application to receive information about notifications. You need to declare the service in your manifest file with the BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action to extend this class. See this example.

I also found this stackoverflow question on how to implement NotificationListenerService using TaskStackBuilder. This might help.

like image 35
abielita Avatar answered Nov 17 '25 09:11

abielita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!