OneSignal / OneSignal-Website-SDK

OneSignal is a push notification service for web and mobile apps. This SDK makes it easy to integrate your website with OneSignal Push Notifications. https://onesignal.com
Other
383 stars 115 forks source link

[Question]: Cant get welcomenotification to trigger #1168

Closed cgar420 closed 2 months ago

cgar420 commented 2 months ago

How can we help?

Here is my component to render OneSignal UI in the app. It works. I can see the slide prompt and subscription bell, and subscribe to push notifications. And I can receive notifications when they are sent from the onesignal dashboard

BUT the welcomeNotification does not trigger. Am I doing something wrong?

This is in Next.js 14 app router. I use <OneSignalUI/> defined below in my Root Layout

"use client"

import { useEffect } from 'react';
import OneSignal from 'react-onesignal';

export default function OneSignalUI() {
    const initializeOneSignal = async () => {
        await OneSignal.init({
            appId: 'xxxxxxxxxxxxxxxxxxx',
            safari_web_id: "xxxxxxxxxxxxx",
            notifyButton: {
                enable: true,
                showCredit: false
            },
            welcomeNotification: {
                disable: false,
                title: "Hello",
                message: "World",
                url: "https://mywebsite.com"
            },
            promptOptions: {
                slidedown: {
                  prompts: [
                    {
                      type: "push", 
                      autoPrompt: true,
                      text: {
                        actionMessage: "Push Notifications are the BEST way to engage & direct your users. Let us show you!",
                        acceptButton: "Show me!",
                        cancelButton: "No thanks"
                      },
                      delay: {
                        pageViews: 1,
                        timeDelay: 5
                      }
                    }
                  ]
                }
              }
        });
        OneSignal.Slidedown.promptPush();
    };

    useEffect(() => {
        initializeOneSignal();
    }, []);

    return (
        <></>
    );
}
jkasten2 commented 2 months ago

@cgar420 Thanks for reporting!

On the OneSignal dashboard, under Settings > Platforms > Web do you have "Typical Site" or "Custom Code"?

If the above doesn't help can you enable VERBOSE logging and share the log here? Also check the network tab to see if it's making a network call to POST onesignal.com/.../notifications. Lastly include the browsers you have tested.

cgar420 commented 2 months ago

@jkasten2 I'm using custom code, not typical site

Im getting this error in the console The FetchEvent for "https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" resulted in a network error response: the promise was rejected.

jkasten2 commented 2 months ago

@cgar420 that file contains the main part of the OneSignal SDK that runs on the page. If that doesn't load nothing will work with OneSignal, won't be prompted, won't make any calls to create the User, etc.

If you are using an ad-blocker make sure to disable it, or use a browser profile without it.

jkasten2 commented 2 months ago

@cgar420 were you able to solve this issue with the details in my last reply?

cgar420 commented 2 months ago

No unfortunately it's still not working.

I get all of the prompts, but don't actually receive any notifications.

In chrome network tab I can see that it fetches a couple things from onesignal. And no errors in the console.

https://webwizard.biz https://GitHub.com/cgar420/WebWizard_v2 has all the site files.

The onesignal sdk is in the /public folder with default service worker path settings. It shows the service worker as active

Then I have a component that initializes onesignal in a use effect

On Tue, Apr 16, 2024, 10:51 AM Josh Kasten @.***> wrote:

@cgar420 https://github.com/cgar420 were you able to solve this issue with the details in my last reply?

— Reply to this email directly, view it on GitHub https://github.com/OneSignal/OneSignal-Website-SDK/issues/1168#issuecomment-2059642466, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUJPGJB7ZSY4RKAR6MYUGYTY5VQLBAVCNFSM6AAAAABGFYFSPSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJZGY2DENBWGY . You are receiving this because you were mentioned.Message ID: @.***>

jkasten2 commented 2 months ago

@cgar420 thanks for sharing your site. I took a look and I see you have another service worker sw.js and it is overwriting the OneSignal service worker. When this happens the OneSignal SDK can no longer process push.

You can add a scope to OneSignal to avoid this issue (set "Service worker registration scope" to something like /onesignal/). See this documentation page on doing so: https://documentation.onesignal.com/docs/onesignal-service-worker-faq

cgar420 commented 2 months ago

I got it working now! Thank you so so much!

On Tue, Apr 16, 2024, 12:47 PM Josh Kasten @.***> wrote:

@cgar420 https://github.com/cgar420 thanks for sharing your site. I took a look and I see you have another service worker sw.js and it is overwriting the OneSignal service worker. When this happens the OneSignal SDK can no longer process push.

You can add a scope to OneSignal to avoid this issue. See this documentation page on doing so: https://documentation.onesignal.com/docs/onesignal-service-worker-faq

— Reply to this email directly, view it on GitHub https://github.com/OneSignal/OneSignal-Website-SDK/issues/1168#issuecomment-2059811611, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUJPGJB3MHG7H5KR572RB6LY5V54JAVCNFSM6AAAAABGFYFSPSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJZHAYTCNRRGE . You are receiving this because you were mentioned.Message ID: @.***>

jkasten2 commented 2 months ago

Good to hear! I'll close this issue as resolved then.