Skip to content
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

addListenerForNotificationOpened callback not working #188

Closed
moklick opened this issue Dec 27, 2016 · 16 comments
Closed

addListenerForNotificationOpened callback not working #188

moklick opened this issue Dec 27, 2016 · 16 comments

Comments

@moklick
Copy link

moklick commented Dec 27, 2016

Hello,

I am encountering problems with the addListenerForNotificationOpened callback.
When I am enabling subscriptions it works whether a tab with my app origin is open or not. But it's only working once. After I clicked a notification it's only working when I don't have open tabs with the origin.
The tab gets focused but the callback is not triggered. When I then close the tab and open it again I receive the callback.

This is how I subscribe to the addListenerForNotificationOpened event:

OneSignal.push(['addListenerForNotificationOpened', notificationData => {
  console.log('Received NotificationOpened:', notificationData);
}]);

I am using chrome 52.0.2743.82 and firefox 48.0.2 on Linux. In both browsers I have the same problems. My workaround for now is that I listen to the messages and check if it's type notification.clicked:

navigator.serviceWorker.addEventListener('message', evt => {
  const { type, payload } = evt.data;
  if (payload && payload.length && type === 'notification.clicked') {
    console.log('Notification clicked:', payload[0]);
  }  
});

Thanks for your help.

@jasonpang
Copy link
Contributor

Hey @moklick,

Do you by any chance have a publicly accessible site URL I could test this out on? Otherwise, can you let us know if:

  • Are you using an HTTPS integration or our HTTP fallback integration?

    If notifications come from subdomain.onesignal.com, then you're using our HTTP integration, otherwise if notifications come from yourdomain.com then you're using an HTTPS integration.

  • Are you using notificationClickHandlerOrigin?

    If you are, you would see this in your JavaScript init options.

I haven't looked into this yet, but it might be related to #185 which is a bug in our SDK.

@moklick
Copy link
Author

moklick commented Dec 29, 2016

Hey @jasonpang,

thanks for your fast response. We are only using HTTPS. I am testing this with localhost and allowLocalhostAsSecureOrigin:true. I tested it with notificationClickHandlerOrigin:'origin' and without that option. I will see If I can setup a demo page.

@TroyCoombs
Copy link

Any update on this?

@jasonpang
Copy link
Contributor

jasonpang commented Jan 28, 2017

Hey @TroyCoombs @moklick,

Based on @moklick's description, what's happening here is that addListenerForNotificationOpened is intentionally only calling the callback once, and not every time a notification is clicked. Unfortunately, this was poorly documented so it wasn't clear. The documentation has been updated now to mention this.

This explains why @moklick mentioned:

  • It only worked once for him, and
  • After working once, it only worked when he didn't have open tabs with the origin. Because, if this happens, we store the event, wait for a new window to be opened from the notification, check for any stored events, find the stored event and fire it.

@moklick, sorry you went through the trouble of testing all that.

For now, to have your callback fired whenever a notification is clicked not just once, but every time, please call addListenerForNotificationOpened() after the callback to addListenerForNotificationOpened() (call it again after your callback has just fired).

@TroyCoombs
Copy link

TroyCoombs commented Jan 30, 2017

Thanks @jasonpang, works great now!

@moklick
Copy link
Author

moklick commented Feb 14, 2017

thanks for the update @jasonpang

@moklick moklick closed this as completed Feb 14, 2017
@farhan687
Copy link

Hey @jasonpang @moklick

I am facing same issue and I never reach to callback Not even once but whenever I refresh the page only then addListenerForNotificationOpened callback is executed.

I am using following code:

I have created test app, I can give you credentials.

var OneSignal = window.OneSignal || [];
    OneSignal.push(["init", {
      appId: "*****",
      subdomainName: '****',
      autoRegister: true, /* Set to true to automatically prompt visitors */
      httpPermissionRequest: {
        enable: true,
      },
      notifyButton: {
        enable: true, /* Set to false to hide */
      },
      notificationClickHandlerMatch: 'origin',
      notificationClickHandlerAction: 'focus',
    }]);

    function notifListen() {
      OneSignal.push(['addListenerForNotificationOpened', notificationData => {
        console.log('Received NotificationOpened:', notificationData);
        notifListen();
      }]);
    }

    notifListen();

I am sending only header and message while pushing the notification.

Did I miss anything?

Let me know if you need appId and REST API key.

@jasonpang
Copy link
Contributor

Hey there,

I wasn't able to reproduce this issue. Make sure the URL you're sending matches the same origin as your site (this means the URL must match the beginning protocol of your site like http:// or https://). Message us at support@onesignal.com with a test page on your site and the URL you're using for the notification if you're still having trouble.

@rluckez
Copy link

rluckez commented Nov 28, 2017

Hi @jasonpang,
I'm facing the issue @moklick describe above, even if i call addListenerForNotificationOpened inside my callback, it just fire once and in a new tab.
I'm using notificationClickHandlerMatch: 'origin' and notificationClickHandlerAction: 'focus'.
I click on notification, tab does get focus, but i do not get notification event.
Am i missing something?

@joaoedezio
Copy link

Hi @rluckez did you find a solution, I have the same issue here.

@rossPatton
Copy link

@jasonpang Can we get a working code example for triggering the click event right away? I've tried to do what was suggested, but it's really unclear what your solution looks like. It might also be helpful to add this solution to the docs - I ended up here because I need to generate dynamic url strings for utm params, and addListenerForNotificationOpened was the suggested solution

@jasonpang
Copy link
Contributor

Hey @rossPatton, can you clarify what you're trying to do? Generating a dynamic UTM param shouldn't be related to addListenerForNotificationOpened. What behavior are you looking for? The callback to addListenerForNotificationOpened should fire the moment the notification is clicked.

@zaidsallam
Copy link

CAN ANYONE GIVE ME THE SOLUTION I FACED THIS PROBLEM BUT I CAN'T FIND THE FIGHT SOL ...PLEASE HELP ME !!! I FACE THE TERMINATION

@emdotem
Copy link

emdotem commented Jul 28, 2018

Having this issue too, any solution?

@meghna-fishbowl
Copy link

Any updates? How can we fix this problem. We get callback just once if we close the browser.

@toanhuynh345
Copy link

use HTTPS in your domain, don't use HTTP. that's work. :D

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