notifications push with one signal and react

Viewed 396

Hello I want to manage push notifications with One Signal however I have the following error

Eror: enter image description here

Config: /public/OneSignalSDKUpdaterWorker.js

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

Config: /public/OneSignalSDKWorker.js

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

Config: screens/login.js

import OneSignal from 'react-onesignal'

useEffect(() => {
    OneSignal.push(function () {
      OneSignal.init({
        appId: '47d0c476-62ad-44c5-8e65-288e54ea9772'
      })
    })
  }, [])
1 Answers

In order to get the push notification working with React, you first need to install the official npm package OneSignal made.

Inside of your project folder, open your terminal and run the following command to install the React OneSignal NPM package.

npm i react-onesignal

Import OneSignal NPM

After you have successfully installed the npm package, open your App.js file, you will enter the following line of code at the top of the file:

import OneSignal from 'react-onesignal';

Use OneSignal NPM

The code above will make the OneSignal object accessible and will allow you to have access to the OneSignal SDK properties. In the same file, we will create a useEffect hook. This hook will have the initialization code needed to trigger OneSignal. Remember to add the dependency array [] to your useEffect hook. The init() method from OneSignal can only be called once and the dependency array will help us to avoid that the useEffect gets triggered multiple times firing the init() method.

useEffect(() => {
   OneSignal.init({
     appId: "YOUR-APP-ID-HERE"
   });
 }, []);

-You can find the working example here -How-to guide (tutorial) here -Video tutorial here

Also, I want to invite you to join our developers' community for the OneSIgnal products. We have a discord serve https://discord.gg/EP7gf6Uz7G

Related