DEV Community

Cover image for Push Notifications in ReactJS with OneSignal

Push Notifications in ReactJS with OneSignal

Pato on January 25, 2021

In this tutorial, you are going to learn how to communicate with the users of your application by integrating push notifications and subscriptions ...
Collapse
 
kannant14 profile image
Kannan T

[Worker Messenger] [Page -> SW] Could not get ServiceWorkerRegistration to postMessage!

Am facing this error any idea why?

Collapse
 
nostin profile image
Sean Thompson

I'm getting this intermittently too.

Collapse
 
kannant14 profile image
Kannan T

@sean Thompson

This issue occurs when you are not subscribed to onesignal, i figured this out yesterday while working @pato you can make a note of this, if some one reported to you regarding this ask them whether they have subscribed or not

Thread Thread
 
devpato profile image
Pato

Awesome! Thanks so much

Thread Thread
 
comorina profile image
Shivam Pandey • Edited

Hey Pato, I am still facing this issue can you give some suggestion that can help me. (User has subscribed already)
Thank you

Collapse
 
kannant14 profile image
Kannan T

check your configuration mostly that should be the problem

Thread Thread
 
devpato profile image
Pato

Did you get it fixed Kannan? Is there something I need to fix from the article?

Thread Thread
 
kannant14 profile image
Kannan T

No pato, your article is fine and it helped me allot, thank you for the article

Collapse
 
kannant14 profile image
Kannan T

Thanks for the article, but how do i test this out in local environment? Is there any way we can do it, because everytime i do some changes i can't push it to production and test it

Collapse
 
kannant14 profile image
Kannan T

And also am facing an issue here, i will post that tomorrow morning as its late night here, your article helped me to start up with one signal thank you

Collapse
 
devpato profile image
Pato • Edited

Hi I need to update the docs but essentially you will do this as the image shown, that way you dont have to keep redeploying our app all the time

dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
kannant14 profile image
Kannan T • Edited

Hi i can't see local testing button from my side attaching screenshot as reference

dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
devpato profile image
Pato

You have to enter a localhost url that way the form will detect it and give you the options you need

Thread Thread
 
kannant14 profile image
Kannan T

Thanks it worked with localhost now, now ill show the issue am facing

dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
devpato profile image
Pato

Did you download and add the sdk files into your project?

5) Inside of the React project, the first thing you are going to do is to add the services worker files you downloaded into your PUBLIC folder.

You should be adding 2 files:

OneSignalSDKUpdaterWorker.js
OneSignalSDKWorker.js

You can download the files here too github.com/OneSignal/OneSignal-Web...

Collapse
 
sathittham profile image
sathittham

Thank you for sharing this. I wonder how can I listen to OneSignal Event with ReactJS (and do something else with that data) after I received the notification.

Collapse
 
devpato profile image
Pato

Hi Sathittham! I want to make sure I'm understanding the question.

1) I send a push notification from the OneSignal portal
2) I received a push notification that has the title and body Ex.

React JS tutorial
Take a look to my new ReactJS tutorial!!

and then in your ReactJS app grab the title of the push notification?

Collapse
 
sathittham profile image
sathittham

Yes, I want to grab data (id, heading, content, data, url, icon) from the notification.

Thread Thread
 
sajideen profile image
Sajideen

Hi @sathittham, I am looking for the same thing. If you have done it, please share the event that was triggered to achieve this.

Collapse
 
ibengeu profile image
ibenge Uforo

Hey, thanks for this article. it was really helpful but ran into a problem.

How can we use this with a PWA having it's own service worker?

Collapse
 
devpato profile image
Pato

You can, you will have to change the scope of the service worker of OneSignal.-

something like this:

OneSignal.SERVICE_WORKER_PARAM = { scope: '/assets/js/' };
OneSignal.SERVICE_WORKER_PATH = '../assets/js/OneSignalSDKWorker.js';
OneSignal.SERVICE_WORKER_UPDATER_PATH = '../assets/js/OneSignalSDKUpdaterWorker.js';
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ibengeu profile image
ibenge Uforo

I'm back again Pato, been on this for weeks.

At the moment, i'm getting "the script resource is behind a redirect which is disallowed" and have tried

  • switching positions of the folder from src to root ie. where React service worker files and it's registration is.
  • Placing the main files without the folder at the root.
  • changing scope url to an absolute path but to no avail.
  • Registering the oneSignal service worker file in place of reacts and using the importScript.

Please help.

Collapse
 
nostin profile image
Sean Thompson

Hi Pato, thank you for this it is working really well but I don't understand this part. In mine the scope is set to /push/onesignal/... what is this doing?

Thread Thread
 
devpato profile image
Pato

Can you show me a screenshot of where you are seeing that, please? Thank you so much!

Collapse
 
jelugom profile image
J. Elias Lugo

Hi, I have a problem, maybe my implementation is wrong, but I want your feedback, I've tried to implement onesignal on my reactjs web app (it's not react native).

I tried 3 differents aproachs:
1.- Following the oficial onesignal web push sdk documentation.
2.- This aproach.
3.- Using this package github.com/pedro-lb/react-onesigna...

All of them I've completed successfully, but all have a strange behavior on my reactjs web app.

When my app get the notifications permissions for first time and I accept it, it doesn't show the welcome notification and if I send a notification from my onesignal dashboard it doesn't show.

If I refresh (F5) my app two times and send another notification, it works and show correctly. But if for any reason I refresh again my app, the notifications don't work anymore and in my onesignal dashboard shows that my player id is unsubscribe.

Somebody has the same issue? or what could be wrong in my app? Thanks.

Collapse
 
devpato profile image
Pato

Hi Elias! Interesting problem. I haven't run into that issue. If you can create a minimal replication of you project and paste the link to your repo here I can try it on my computer.

what browser are you using?

Collapse
 
danishalikhan688 profile image
danishalikhan688

window.OneSignal = window.OneSignal || [];
Showing error in app.tsx

Collapse
 
pcast01 profile image
Paul

I used this code from: stackoverflow.com/questions/564579...

declare global {
interface Window {
OneSignal: any;
}
}