Adding Push Notifications to Your Ionic React App
The following is guest blog post from Pato Vargas, Developer Advocate at OneSignal.
It’s no secret that push notifications can help you engage and retain app users. This tutorial will show you how to integrate with OneSignal to leverage push notifications in your ReactJS app.
OneSignal & Your Browser’s Push API
Your browser’s Push API gives web applications the ability to receive messages from a server whether the web app is in the foreground or currently loaded on a user agent. This lets you deliver asynchronous notifications and updates to users who opt-in, resulting in better engagement with timely new content.
This tutorial will cover how to integrate the new React OneSignal NPM package to add push notifications into your app using our typical setup process. Part one of this guide covers the OneSignal setup process. Part two of this guide covers how to integrate OneSignal with React using our npm package. This tutorial requires some basic knowledge of React. I’m using the Ionic CLI to generate my project and NodeJS version 14.16.
Additional Resources
Part 1: Set Up Your OneSignal Account
To begin, log in to your OneSignal account or create a free account. Then, click on the blue button entitled New App/Website to configure your OneSignal account to fit your app or website.
Insert the name of your app or website. Select Web Push as your platform.
Click on the blue button entitled, Next: Configure Your Platform.
Web Configuration
Under Choose Integration, select the Typical Site option.
In the Site Setup section, enter your chosen web configuration. In my case, the configuration looks like this:
Notice for testing purposes I’m entering my localhost URL (http://localhost:3000). If you are doing the same, make sure you click on the LOCAL TESTING option. This will ensure to treat HTTP localhost as HTTPS for testing.
Under Permission Prompt Setup, you will see three vertical blue dots under the Actions header on the far right side of the screen. Click on the blue dots and select Edit from the drop-down menu.
A window will open with the configuration of our push notification Slide Prompt. Confirm that Auto-prompt is enabled (toggled to the right).
Under Show When, you can change the second increment to alter how long your slide prompt will delay after a user visits your page. You can leave it as it is, or you can reduce the seconds so that your prompt appears sooner. Once you’ve input your chosen delay increment, click the grey Done button located at the bottom right corner of the window.
After clicking Done, scroll down to the bottom of the page and click Save to save your auto-prompt selections.
You will be redirected to a different page with an important step: Downloading the SDK files. Click DOWNLOAD ONESIGNAL SDK FILES and save them on your computer to retrieve later.
Under the section entitled Add Code to Site, you will see an APP ID, copy that APP ID and save it somewhere in your computer.
Part 2: Quick Push Notification Setup In Ionic React
Creating Your Ionic App
Inside your terminal, you will have to run the following command to add the Ionic project globally in your machine to use Ionic in your command line.
npm install -g @ionic/cli
Inside your terminal, run the following command to create a new Ionic React project using the Ionic CLI. You will be asked to select a framework. Select React.
ionic start
When asked to enter a project name, you can enter whatever name you’d like. In my example, I named the project OneSignal-Ionic.
Later you will be asked to select a template, feel free to select the template that you want. In my example, I selected tabs.
Adding OneSignal To Your Ionic Application
In your Ionic project folder, navigate to the public folder and locate the SDK files you downloaded on your computer and insert them inside your Ionic app’s public folder.
If you need to, you can re-download the OneSignal SDK files.
Install React-OneSignal NPM package
Inside your project folder, open your terminal and run the following command to install the React OneSignal NPM package.
npm i react-onesignal
After you have successfully installed the npm package, open your App.tsx file, you will enter the following line of code at the top of the file:
import OneSignal from "react-onesignal";
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"
});
}, []);
In the appId propety insert the OneSignal App ID you saved somewhere in your computer.
Allowing Web Push Notifications
Run the Ionic app and visit your website. You should see the following prompt appear after your chosen time delay interval:
Click on the blue Subscribe button and click Allow in the following native prompt that appears to enable web push notifications on your browser.
Note: Refresh the Ionic app in your browser, if you don’t see the service worker registered after doing these steps.
Sending Web Push Notifications
It’s time to send your first web push notification! To do so, log in to your OneSignal account and navigate to the Dashboard tab. On the dashboard page, click on the blue button that says New Push.
You will be redirected to a new window that will allow you to customize your push notification. Under Audience, make sure that Send to Subscribed Users is selected. Then, create your message by adding your message title, content, and image. Because this is the first notification your subscribers will receive, you may choose to craft a simple welcome message to confirm that they’ve been subscribed and reinforce the value that notifications will provide.
Under the Delivery Schedule section, select Immediately and Send to everyone at the same time to send to all your current web push subscribers. If you have just finished setting up your OneSignal account, chances are you’re the first and only subscriber. If your app or website is heavily trafficked and other users have already opted in to receive push notifications, you may want to select Send to a particular segment(s) to test your message out on a specific audience. When you’re ready to send your message, click on the blue Review and Send button at the bottom of the screen.
A small pop-up will appear for you to review your message. Once you are satisfied, click on the blue Send Message button. You should receive a web push notification on your device! 🚀
Now, you can keep expanding your code to make use of different features of the OneSignal NPM across your React app by passing the OneSignal
variable to different components. You can also use the custom code setup to modify the configurations of the prompt inside your React application without using the OneSignal dashboard. To learn more about the Web Push SDK visit our web push SDK documentation.
To learn more about the Web Push SDK visit our web push SDK documentation. Still, have questions? Connect with us on GitHub or email us at support@onesignal.com.