Push notification - how to reliably detect when you tap the notification vs. tap the app icon


#1

Not really an ionic issue, but hopefully folks here have some advice to share.
I’ve been trying to solve how to “reliably” detect when a user taps on a notification vs. tapping on the app icon. The detection needs to reliably work across the following situations:

  1. App is in foreground (trivial case, works well)
  2. App is in background, user taps on notification vs user taps on app icon (works well, I’ve realized that in this case if you tap on a notification, a “dismissed” flag will be added)
  3. App is killed, user taps on a notification vs. user taps on icon - I’ve been struggling trying to solve this.

I am currently using https://github.com/phonegap/phonegap-plugin-push and I can’t seem to find a reliable way to do this detection. I’ve noted behavior here and here

I’ve tried force-start as well. It seems the alternate plugin, cordova-plugin-fcm hasn’t been updated for 6 months and has compilation issues.

So for folks who are doing push related work, and have handled item 3, how are you doing it?


#2

Problem solved. I was making it unnecessarily complicated.

Reliable process (tested on android):

  1. Remove content-available in payload to avoid duplicate notification callback mess
  2. In on('notification') the following set of conditions means you tapped on a notification - works for app in background or killed
if (data.additionalData.foreground == false &&
   (data.additionalData.dismissed != undefined || 
   data.additionalData.coldstart == true))
{ 
   // user tapped on notification
}
  1. Remember that ‘notification’ handler can be called anytime after your app has finished initing - there is no guarantee the callback will be invoked at a particular time relative to the rest of the app bootstrap - its an async event and there are no guarantees on how long it takes to get called with respect to the rest of the state of your app. This is where I was messing up. So I am emitting an event here so its handled whenever it is called

#3

Hi, thanks for your solution. I have the same issue as you described previously but if i remove the content-available parameter, and the app is killed, if i open the app through the icon the notification is not processed, and is being lost when the app is closed. Have you treated that case by any chance?
Thank you.


#4

Hi, to be honest, I haven’t investigated further. You may be right - but given no one has complained yet, I moved on to other things. In that case, you need to write logic to handle double notifications (I believe the push plugin documentation does discuss this, but I forgot - its been a while)