Skip to content

notificationclose fired but notificationclick dose not fire #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
14kw opened this issue Feb 8, 2017 · 14 comments
Closed

notificationclose fired but notificationclick dose not fire #102

14kw opened this issue Feb 8, 2017 · 14 comments

Comments

@14kw
Copy link

14kw commented Feb 8, 2017

Page with the URL specified by clickaction does not contain firebase-messaging.js.
So I want to obtain log with notificationclick rather than onmessage.
Notificationclose fired, but notificationclick did not fire.

How can I fire notificationclick?

push body

{ "notification": {
    "title": "Background New Topic Title",
    "body": "Background New Topic body",
    "click_action" : "https://google.com"
  },
  "to" : "xxxx"
}

firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

firebase.initializeApp({
  'messagingSenderId': 'xxxxx'
});
const messaging = firebase.messaging();

// not fire
self.addEventListener("notificationclick", function(event) {
  console.log('notification open');
  // log send to server
});

// fired
self.addEventListener("notificationclose", function(event) {
  console.log('notification close');
  // log send to server
});

Google Chrome 56.0.2924.87

@14kw 14kw changed the title notificationclose dosed fired but notificationclick dose not fire notificationclose fired but notificationclick dose not fire Feb 9, 2017
@14kw
Copy link
Author

14kw commented Feb 9, 2017

The issue was resolved.

Reason for not firing:

firebase-messaging.js has the following code.

self.addEventListener("notificationclick", function(event) {
  event.stopImmediatePropagation();
  event.notification.close();

lib nofiticationclick-function was executing stopImmediatePropagation().
my notificationclick-function has been defined since the library was executed.
So my notificationclick-function will not fire.

Resolution:

Define my notificationclick-function before the library is loaded.

self.addEventListener("notificationclick", function(event) {
  console.log('notification open');
  // log send to server
});

importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

firebase.initializeApp({
  'messagingSenderId': 'xxxxx'
});
const messaging = firebase.messaging();

@14kw 14kw closed this as completed Feb 9, 2017
@heygambo
Copy link

@14kw I was trying to figure this out the whole day and I was about to give up!
Thx for sharing your solution!

@sibelius
Copy link

should this be considered a bug?

@pebojote
Copy link

That gives me an idea

@prathapGunasekaraAscentic

@14kw You are lifesaver

@sunderls
Copy link

sunderls commented Oct 4, 2020

oh my god thanks for this...

@sniffymon
Copy link

Damn, 5 hours wasted and it could've been fixed by moving the import scripts. -_-

@ghost
Copy link

ghost commented Oct 29, 2020

Still doesn't work for me :(

@muhammadaqibmasood
Copy link

still notworking forme

@Transporterapps
Copy link

Worked for me, Thanks for the Solution, You are a life saver

@bashoogzaad
Copy link

Thank you very much! Just like the other people this has cost me many hours. I think Firebase should document this behaviour.

@guimap
Copy link

guimap commented May 15, 2022

Doesn't worked for me, anyone can tell me which version this approach works ?

@dikshatickoo1991
Copy link

Hi, It is not working for me. Before importing the scripts I used self.addEventListener("notificationclick", function(event) {
console.log('notification open');
// log send to server
}); But log statement is not printing

@davbrito
Copy link

Currently the SDK sends the case of the notification click to the window (https://github.com/firebase/firebase-js-sdk/blob/15addde0b9d56dd3490e9c3c1e2f015e10e156cb/packages/messaging/src/listeners/sw-listeners.ts#L162)
But, on the window side it doesn´t handle the case.
(https://github.com/firebase/firebase-js-sdk/blob/15addde0b9d56dd3490e9c3c1e2f015e10e156cb/packages/messaging/src/listeners/window-listener.ts#L29-L57)

One could do that on the userland but the library stops the propagation of the notificationclick event.

I think they should add a handler like "onNotificationClick" so the user could handle easily the click on the window side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests