Detect when Notification is clicked to send analytic's event - android

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

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

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.

Related

How to get Android push notification payload when background App is launched by user click on notification

I have an Android app that's in the background. I've registered it with Firebase and have been able to successfully retrieve it's token, as well as sending messages to it via the Firebase Console Manager (FCM).
When I send test messages via FCM, when the app is in the foreground the
onMessageReceived(remoteMessage:RemoteMessage)
call back API for my FirebaseMessagingService derived class gets triggered. I can then call remoteMessage.notification!!.body and obtain it's payload. This is the documented behavior so it's working as expected.
When the app is in the background though (say I press the home key), and I send the test message, that event doesn't get called but instead a notification shows up on the Android device, which is also the expected behavior. So far so good.
However, when I click on the notification in the notification channel, it brings the app to the foreground. However, when the app is launched like that - how does one retrieve the push notification payload? The onMessageReceived isn't triggered, and I'm having a hard time finding the answer to this in Android's documentation.
Any indication as to how to obtain that?
Thanks
Turns out this question provided the answer i was looking for. TL;DR - look in the extras field in the intent variable of the Activity
Firebase (FCM): open activity and pass data on notification click. android
Im not sure my solution below is best. But I think you can save the message to share preferences . And get it to another place it you want to you.
override fun onMessageReceived(p0: RemoteMessage) {
//TODO save it to share preferences
}
Give me another better solution if you find it. Thank you so much.

Handling fcm notification when app not launched

I am using fcm push notification for my android app. I was able to display push notification on system tray when app is not launched. When I tap on the notification it opens the app launcher by default and I start an activity A from there. But the issue is, if I put the app to background and click on the app icon it again opens the app launcher rather than opening existing Activity A.
If the app process is killed, start the launcher activity. If the app is in the background, you can pass an intent to the notification which starts a DummyActivity that has no code on it, and immediatelly calls finish() on its onCreate() method. This will bring your app to the foreground.
Several things are not clear in your question. For example: How you send messages (from developer console or through rest api post requests to firebase backend)? What is your desired behaviour for app when push messages come? I will try to give you general answer that probably helps you to address issue and understand how to implement desired behaviour.
In any case, there are two types of Firebase push messages:
data messages
notification messages
more details about it check on Notification & data messages page
If you want to send additional details to activity that you are starting (something similar to bundle extras), you should use data messages and handle those in your service that extends FirebaseMessagingService by overriding onMessageReceived(RemoteMessage remoteMessage) method. This method is preferable for me because it is much more flexible. You can define all the details about showing notification based on received firebase message, including if notifications are bundled, what happens in details when user click notification and almost everything related to it.
If you don't need to start certain activity with some parameters, than you can use push messages and just define click_action. This method allows you to add define title, text and sound of notification (beside some other details) but it is not as flexible as if you send data messages
Here you can find detailed overview of possible parameters that you can use for different type of messages
Hope this helps

Android receive notification open and cancel event

I receive data from my webService to generate custom notifications.
I want to track Intent to be aware of open (click) or cancel (swipe) event on a notification , to report server for analytics.
Is there any listener for onIntentStart or onIntentCanceled ?
Maybe a listener for notifications by notificationId ?
Edit :
i want to do this without changing user's contentIntent or DeleteIntent or asking user to add lines of code to NotificationHandlerActivity !
You can set contentIntent and deleteIntent using setContentIntent/setDeleteIntent. To find more please visit: Building a Notification.
In addition you can subclass NotificationListenerService which is a service that receives calls from the system when new notifications are posted or removed, or their ranking changed.
Here you can find example how to use it.

Handling Parse Push Notifications in Android

I am using Parse API in order to handle push notifications. In our Android application, I want to accomplish two things:
1) If we have received a Push Notification with the application is closed and the user clicks on the notification, I want to be able to understand that the application is being opened via a push notification.
2)If we receive a push notification while the application is open, I want to handle this and do some extra work.
In both cases, I want to be aware that the application has received a push notification in order to execute some special operations.
As far as I understand from Parse API documentations, it offers two methods of handling pushes: Responding with an Activity and Responding with an Intent. I am currently calling
PushService.setDefaultPushCallback(context, MainActivity.class);
in my Application class with needed changes in the AndroidManifest.xml file and already receive push notifications, this corresponds to Responding with an Activity method. But I don't know how to be aware of Push Notifications explicity with this method.
Thanks in advance.
When a push is received ,Check
1:Whether our application is in foreground or background.
If it is foreground, that means app is visible and do your stuff(show alerts or anything you want).
If app is in background,that means it is not visible and if you want to do any thing based on this.
i hope this helps..

Android notifications actions without opening the app

I want to implement a notification action in JB. But it shouldn't open the app. The behaviour is similar to the Gmail apps delete notification action. How can I do this?
Thanks in advance,
Shashika
After some research I managed to achieve this using a broadcast receiver. I posted the example code here. http://shashikawlp.wordpress.com/2013/05/08/android-jelly-bean-notifications-with-actions/
You can also add custom buttons or layouts with onClickEvents to your notification compat (e.g. events which do not open the app ;)):
Create a custom RemoteViews Layout and add it to the builder builder.setContent(remoteView);
In this layout you can define Buttons
Set an onClickPendingIntent to your control remoteView.setOnClickPendingIntent(R.id.button, pendingIntent);
The PendingIntent can hold a BroadcastReceiver to trigger an action without jumping INTO the app
Happy coding
From what I understand of what your asking, you need to run a AlarmManager that launches background services at specified times to create notifications, and then you need to specify the intent for the notification to do what you want it to do.
If you are getting Push notifications, then forget the AlarmManager and just launch the action you want to do from the intent in the notification.
For more specific answer we need a more specific question.

Categories

Resources