havesource / cordova-plugin-push

Register and receive push notifications
MIT License
149 stars 283 forks source link

Call .on('notification') when app is in background #67

Open esanchezvz opened 3 years ago

esanchezvz commented 3 years ago

Bug Report

Disclaimer - this is not a bug report. I just need some guidance to get a feature involving this plugin to work correctly.

I'm building an app with Ionic + Cordova + Angular I and we have a feature that needs to redirect users to a specific screen when clicking on a push nootification. We are using firebase cloud messaging to send out the notifications.

Expected Behaviour

We send out a notification with a new post created and the user clicks on that notification from the tray and it should open the app + navigate to that specific screen.

Actual Behaviour

This works perfectly whenever the app is running in the foreground, but when my app is closed or in the background the event handler is not called.

Reproduce Scenario (including but not limited to)

To reproduce this you would just have to add this pluggin and send out a push notification with a payload to see that the events are not firing.

Sample Push Data Payload

{
  "sound": "default",
  "title": "My notification title",
  "message": "Some message to tell the user",
  "additionalData": {
    "path": "post/1096",
    "color": "#fff",
    "dismissed": false,
    "coldstart": false,
    "foreground": false
  }
}

I was looking through the repo and found that if I send a notification with a payload it would not trigger the .on('notification') event handler, but it also says that if I add the content-available: "1" parameter to the payload it would trigger all events in the background. However, I can't test if by adding content-available it would work or how it would behave since I don't have control over the backend (I've already told this to the backend team) but apparently it may take a while to add this for me to test. So I was wondering if you could tell me if that would be the correct approach or how should I go about getting this feature to work.

I've already added deeplinks logic to handle app routing but I can't get the .on('notification') event to run when my app is on the background. Is there a way to do this?

stardevrk commented 3 years ago

@esanchezvz Please help me. What ionic-native wrapper did you use with this cordova plugin. I am working on the ionic project with this plugin. But I can not use this plugin fully without the ionic native wrapper. Thanks.

Heshyo commented 3 years ago

@stardevrk This plugin should be compatible with npm i @ionic-native/push.

francobasaglia commented 3 years ago

@stardevrk @esanchezvz to execute .on('notification') callback, when app isn't on foreground, you should add "content-available": "1" on notification payload. The behaviour could not be the same on iOS and Android. Take a look here:

https://github.com/havesource/cordova-plugin-push/blob/master/docs/PAYLOAD.md#ios-behaviour