4

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

Sreehari
  • 5,621
  • 2
  • 25
  • 59
FVod
  • 2,245
  • 5
  • 25
  • 52

2 Answers2

1

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
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • 3
    Hi, this is what we were trying to avoid, is there another option (such as a listener or receiver)? – FVod Feb 08 '16 at 13:39
  • there is a flaw: when you restart your activity (for example by changing the orientation) the onCreate will be called with the same intent and you will track the "open from notification" twice. – Frank Mar 27 '18 at 08:27
1

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.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
  • Thank you very much, the problem about using NotificationListenerService is that this is introduced on API 18, but we support API 16. – FVod Feb 09 '16 at 07:51