简体   繁体   English

react-onesignal:通知提示后订阅 Onesignal

[英]react-onesignal: subscribing to Onesignal after notification prompt

I've been trying for a while to integrate OneSignal with my React project.我一直在尝试将 OneSignal 与我的 React 项目集成。 After finding the package react-onesignal I decided to give it a go.在找到 package反应信号后,我决定给它一个 go。

It is integrated in my code like this:它像这样集成在我的代码中:

    OneSignal.initialize("my_appid_is_here", {
      allowLocalhostAsSecureOrigin:true,
      autoRegister:true,
      autoResubscribe:true,
      persistNotification:true,
    })

  useOneSignalSetup(async() => {
    if (OneSignal.isPushNotificationsEnabled) {
      const instance = OneSignal.getOneSignalInstance()
      const state = await OneSignal.getNotificationPermission()
      if (state !== "granted") {
        await OneSignal.registerForPushNotifications()
      } else {
        OneSignal.setSubscription(true)
      }
    }
  })

I am able to trigger the registration prompt in the browser (I get the 'would you like to enable notifications' alert), but when I try and check the "Audience" in OneSignal it still shows up as empty.我能够在浏览器中触发注册提示(我收到“您是否愿意启用通知”警报),但是当我尝试检查 OneSignal 中的“受众”时,它仍然显示为空。

Do you have any idea how I could about this?你知道我该怎么做吗?

Thanks in advance提前致谢

While we appreciate that package created by the author, that package is not supported by OneSignal.虽然我们感谢作者创建的 package,但 OneSignal 不支持 package。 I have created this guide on how to integrate OneSignal into your ReactJS app.我创建了本指南,介绍如何将 OneSignal 集成到您的 ReactJS 应用程序中。

https://dev.to/devpato/push-notifications-in-reactjs-with-onesignal-5bon https://dev.to/devpato/push-notifications-in-reactjs-with-onesignal-5bon

You will use the cdn:您将使用 cdn:

    <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>

Initialize OneSignal初始化 OneSignal


window.OneSignal = window.OneSignal || [];
const OneSignal = window.OneSignal;

OneSignal.push(()=> {
      OneSignal.init(
        {
          appId: "1a9bbed2-9294-4192-a736-01b461cb389a", //STEP 9
          promptOptions: {
            slidedown: {
              enabled: true,
              actionMessage: "We'd like to show you notifications for the latest news and updates about the following categories.",
              acceptButtonText: "OMG YEEEEESS!",
              cancelButtonText: "NAHHH",
              categories: {
                  tags: [
                      {
                          tag: "react",
                          label: "ReactJS",
                      },
                      {
                        tag: "angular",
                        label: "Angular",
                      }
                  ]
              }     
          } 
        },
        welcomeNotification: {
          "title": "One Signal",
          "message": "Thanks for subscribing!",
        } 
      },
      );
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM