quickstart-js icon indicating copy to clipboard operation
quickstart-js copied to clipboard

how to handle foreground click notification event

Open anwaarmirza opened this issue 9 years ago • 26 comments

i want the notification click event using

messaging.onMessage...

in foreground

anwaarmirza avatar May 10 '17 11:05 anwaarmirza

notification clicks are passed to the service worker file.

You would need to add a click event listener as shown here: https://web-push-book.gauntface.com/chapter-05/03-notification-behaviour/#notification-click-event

gauntface avatar May 26 '17 19:05 gauntface

@gauntface I tried the above link and found out that event doesn't contain click_action

nimitbhargava avatar Jun 14 '18 05:06 nimitbhargava

The "click_action" is a FCM specific field that they implemented inside FCM JS SDK. In their implementation, they store the whole payload in notificationOptions' "data" field, so that they can retrieve it inside the "notificationclick" event handler.

So, instead of:

var options = { 
  body: payload.notification.body, 
  icon: payload.notification.icon, 
  click_action: payload.notification.click_action 
}; 

We should use:

var options = { 
  body: payload.notification.body, 
  icon: payload.notification.icon, 
  click_action: payload.notification.click_action, // To handle notification click when notification is moved to notification tray
  data: {
    click_action: payload.notification.click_action
  }
};

Github code for reference - https://github.com/firebase/firebase-js-sdk/blob/27d27789ad7cb6fc157f60488e151d7f83f710e8/packages/messaging/src/controllers/sw-controller.ts#L242

Note: The FCM documentation misses this specific information.

nimitbhargava avatar Jul 09 '18 02:07 nimitbhargava

Hallo Everyone, i would like to implement an click on notification when the app is foreground (it works when the app is background) but not if the app foreground, am using fcm by (firebase cloud function), thanks for everyone

MasriNaser avatar Nov 09 '18 11:11 MasriNaser

@MasriNaser Did you find a solution to this? I'm stuck here.

krishnan-ubuntu avatar Sep 22 '19 14:09 krishnan-ubuntu

@krishnan-ubuntu you can check my comment here for the solution.

nimitbhargava avatar Sep 23 '19 07:09 nimitbhargava

@krishnan-ubuntu you can check my comment here for the solution.

Thank you, but I got it working.

krishnan-ubuntu avatar Sep 30 '19 10:09 krishnan-ubuntu

@krishnan-ubuntu Can you share your implementation if it's different from the suggested one?

nimitbhargava avatar Sep 30 '19 11:09 nimitbhargava

@nimitbhargava yes, I will create a new repo and share it.

krishnan-ubuntu avatar Oct 08 '19 06:10 krishnan-ubuntu

@krishnan-ubuntu you can check my comment here for the solution.

Thank you, but I got it working.

Can you help me in getting the click_action to work in the background in firebase web

BharadhanVM avatar Oct 09 '19 12:10 BharadhanVM

@krishnan-ubuntu Can you share your implementation if it's different from the suggested one?

Have you got it working in the background

BharadhanVM avatar Oct 09 '19 12:10 BharadhanVM

@BharadhanVM

Can you help me in getting the click_action to work in the background in firebase web

You can share your repo here or mail me

nimitbhargava avatar Oct 09 '19 12:10 nimitbhargava

Here is my Code . I am sending the notification from the firebase console. When I am sending a post request from the postman and clicking on the notification it redirects me to the URL. But when I am sending it from the firebase console it is not working.

On Wed, Oct 9, 2019 at 5:58 PM Nimit Bhargava [email protected] wrote:

@BharadhanVM https://github.com/BharadhanVM

Can you help me in getting the click_action to work in the background in firebase web You can share your repo here or [mail me](mailto: [email protected])

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-js/issues/137?email_source=notifications&email_token=AKQ3OVOYOZ5KFYGF5Q7KTO3QNXE5NA5CNFSM4DK2EDAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAXWUBY#issuecomment-539978247, or mute the thread https://github.com/notifications/unsubscribe-auth/AKQ3OVOR2EE6SOA4VF7NU33QNXE5NANCNFSM4DK2EDAA .

BharadhanVM avatar Oct 09 '19 12:10 BharadhanVM

My repository Link https://github.com/BharadhanVM/Firebase-Web-Notifications.git

On Wed, Oct 9, 2019 at 6:14 PM Bharadhan Deepan < [email protected]> wrote:

Here is my Code . I am sending the notification from the firebase console. When I am sending a post request from the postman and clicking on the notification it redirects me to the URL. But when I am sending it from the firebase console it is not working.

On Wed, Oct 9, 2019 at 5:58 PM Nimit Bhargava [email protected] wrote:

@BharadhanVM https://github.com/BharadhanVM

Can you help me in getting the click_action to work in the background in firebase web You can share your repo here or [mail me](mailto: [email protected])

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-js/issues/137?email_source=notifications&email_token=AKQ3OVOYOZ5KFYGF5Q7KTO3QNXE5NA5CNFSM4DK2EDAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAXWUBY#issuecomment-539978247, or mute the thread https://github.com/notifications/unsubscribe-auth/AKQ3OVOR2EE6SOA4VF7NU33QNXE5NANCNFSM4DK2EDAA .

BharadhanVM avatar Oct 09 '19 12:10 BharadhanVM

@BharadhanVM your repo has nothing but empty readme 😅 I guess you should create a StackOverflow question instead of this thread.

nimitbhargava avatar Oct 09 '19 13:10 nimitbhargava

Can you check this branch https://github.com/BharadhanVM/Firebase-Web-Notifications/tree/notification-feature . It has the files please

On Wed, Oct 9, 2019 at 6:37 PM Nimit Bhargava [email protected] wrote:

@BharadhanVM https://github.com/BharadhanVM your repo has nothing but empty readme 😅 I guess you should create a StackOverflow question instead of this thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-js/issues/137?email_source=notifications&email_token=AKQ3OVLKRJRNCGZ3HMWJ6N3QNXJSFA5CNFSM4DK2EDAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAX2EWY#issuecomment-539992667, or mute the thread https://github.com/notifications/unsubscribe-auth/AKQ3OVOOSZPWGOLM5R6SGUTQNXJSFANCNFSM4DK2EDAA .

BharadhanVM avatar Oct 09 '19 13:10 BharadhanVM

@nimitbhargava Here is my repository link https://github.com/BharadhanVM/Firebase-Web-Notifications/tree/notification-feature

BharadhanVM avatar Oct 09 '19 13:10 BharadhanVM

@BharadhanVM Can you share the payload information from here

Also, I would refrain from using this thread to solve implementation related issue as it's not FCM issue. I would suggest StackOverflow to ask.

nimitbhargava avatar Oct 09 '19 13:10 nimitbhargava

There is nothing printed on the payload ,I am not able to find it on the stackoverflow.Can I have your contact details if possible

On Wed, 9 Oct 2019 at 7:26 PM, Nimit Bhargava [email protected] wrote:

@BharadhanVM https://github.com/BharadhanVM Can you share the payload information from here https://github.com/BharadhanVM/Firebase-Web-Notifications/blob/notification-feature/messaging/public/index.html#L141

Also, I would refrain from using this thread to solve implementation related issue as it's not FCM issue. I would suggest StackOverflow https://stackoverflow.com/questions/ask to ask.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-js/issues/137?email_source=notifications&email_token=AKQ3OVIXQT5626WPCJKG7CDQNXPKBA5CNFSM4DK2EDAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAX674Y#issuecomment-540012531, or mute the thread https://github.com/notifications/unsubscribe-auth/AKQ3OVMH6MLBMJ2XANSP7RLQNXPKBANCNFSM4DK2EDAA .

BharadhanVM avatar Oct 09 '19 13:10 BharadhanVM

@BharadhanVM It could be a possible reason for you to not receive notification when app is on foreground. Please go ahead and create StackOverflow question (from here) and then share the link here. Also tag it with firebase-cloud-messaging so it can be discoverable by the community.

Make sure you follow How to create a Minimal, Reproducible Example

nimitbhargava avatar Oct 09 '19 14:10 nimitbhargava