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

[Question]: One signal notifications are not working in edge browsers? #1205

Open
krishnacoder-02 opened this issue Nov 13, 2024 · 1 comment

Comments

@krishnacoder-02
Copy link

krishnacoder-02 commented Nov 13, 2024

How can we help?

I have integrated one signal + next.js with one signal package I am getting notifications in chrome browser but not receiving notifications in edge browser.

this is my one signal implementation.
useEffect(() => {
router.events.on("routeChangeStart", () => NProgress.start());
router.events.on("routeChangeError", () => NProgress.done());
router.events.on("routeChangeComplete", () => NProgress.done());

const initOneSignal = async () => {
  if (!isLoggedIn) {
    console.log("User is not logged in, skipping OneSignal initialization.");
    return;
} 
  //debugger;
  const oneSignalExternalId = await fetchCustomerProfile();
  
  
  if (typeof window !== "undefined" && isLoggedIn && !isOneSignalInitialized.current && oneSignalExternalId && router.pathname != "/") {
    isOneSignalInitialized.current = true;
    console.log("Initializing OneSignal...");
    await OneSignal.init({
      appId: ONESIGNAL_APP_ID,
    //  safari_web_id: ONESIGNAL_SAFARI_WEB_ID,
      //serviceWorkerPath: '/OneSignalSDKWorker.js',
      //serviceWorkerParam: {scope:"/"},
      notifyButton: {
        enable: true,
      },
    });
    console.log("OneSignal initialized.");
   // console.log('OneSignal.setExternalUserId(oneSignalExternalId) ',OneSignal.setExternalUserId(oneSignalExternalId));
    
  // Ensure OneSignal is available before calling login
  if (OneSignal && typeof OneSignal.login === 'function') {
    await OneSignal.login(oneSignalExternalId);
    console.log(`User logged into OneSignal with external ID: ${oneSignalExternalId}`);
  } else {
    console.error("OneSignal.login is not a function");
  }

    const notifications = OneSignal.Notifications;

    if (notifications.isPushSupported()) {
      console.log("Push notifications are supported.");

      // Check if the user has already subscribed to notifications
      const pushSubscription = OneSignal.User.PushSubscription;
      console.log('User subscription: ', pushSubscription.optedIn);

      if (pushSubscription.optedIn) {
        console.log("User is already subscribed.");
        setIsSubscribed(true);
      } else {
        console.log("User is not subscribed. Showing popup.");
        setShowPopup(true);
      }

      // Add event listener to handle subscription change
      pushSubscription.addEventListener("change", (change) => {
        console.log("Subscription status changed:", change.current.optedIn);
        if (change.current.optedIn) {
          setIsSubscribed(true);
          setShowPopup(false);
        } else {
          setIsSubscribed(false);
        }
      });
    } else {
      console.log("Push notifications are not supported in this browser.");
    }
  }
};
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/OneSignalSDKWorker.js?v=2')
    .then(registration => {
      console.log('Service Worker registered with scope:', registration.scope);

      if (registration.waiting) {
        registration.waiting.postMessage('skipWaiting');
      }

      navigator.serviceWorker.addEventListener('controllerchange', () => {
        console.log('New service worker activated');
      });
    })
    .catch(error => {
      console.error('Service Worker registration failed:', error);
    });
}

initOneSignal();
return () => {
  console.log("OneSignal cleanup");
  initOneSignal();
}

}, [isLoggedIn, router.pathname]);

@sherwinski
Copy link
Contributor

sherwinski commented Nov 15, 2024

Hey @krishnacoder-02, can you provide a bit more information so that I can investigate this further:

  • version of next.js that you are using
  • either a link to your website or a repository that reproduces this issue

Thanks!

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

2 participants