<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: William Shepherd</title>
    <description>The latest articles on DEV Community by William Shepherd (@iamwillshepherd).</description>
    <link>https://dev.to/iamwillshepherd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F150427%2Fbe540706-c58f-442d-a95a-6e25be3e34f3.jpeg</url>
      <title>DEV Community: William Shepherd</title>
      <link>https://dev.to/iamwillshepherd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamwillshepherd"/>
    <language>en</language>
    <item>
      <title>How to Add Push Notifications to a Vue App</title>
      <dc:creator>William Shepherd</dc:creator>
      <pubDate>Wed, 22 Dec 2021 23:08:44 +0000</pubDate>
      <link>https://dev.to/onesignal/how-to-add-push-notifications-to-a-vue-app-mke</link>
      <guid>https://dev.to/onesignal/how-to-add-push-notifications-to-a-vue-app-mke</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wuv5LDEG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://onesignal.com/blog/content/images/2021/12/add-push-notifications-to-a-vue-app-4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wuv5LDEG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://onesignal.com/blog/content/images/2021/12/add-push-notifications-to-a-vue-app-4.jpg" alt="How to Add Push Notifications to a Vue App" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This guide will help you quickly add push notifications to your Vue application (for free) with the OneSignal-Vue plugin. To follow this tutorial, you will need to have some working knowledge of Vue 2 and its tooling. The focus of this guide is to demonstrate how to push a notification to a Vue app. It does not cover how to create a new Vue app; for that, please refer to Vue's documentation on &lt;a href="https://cli.vuejs.org/guide/creating-a-project.html"&gt;how to create a new Vue project&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guide Overview
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Part 1: Set Up Your OneSignal Account&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Web Configuration&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Part 2: Configure Your Vue Application&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Initializing the OneSignal Vue Plugin&lt;/li&gt;
&lt;li&gt;Using the Plugin on Components&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Share Your Feedback&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Part 1: Set Up Your OneSignal Account&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To begin, &lt;a href="https://app.onesignal.com/login"&gt;log in&lt;/a&gt; to your OneSignal account. If you don't have a OneSignal account, you can easily &lt;a href="https://app.onesignal.com/signup"&gt;create a free account&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Web Configuration
&lt;/h3&gt;

&lt;p&gt;To begin, log in to your OneSignal account and click the _ &lt;strong&gt;New App/Website&lt;/strong&gt; _ button on the dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---TEX36Y8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/30rSEw3USzJOb2Xt7gr_AH9M7dWQQBYBWOOfCwT4oVNJAowzpvDhbeKThTIpSp-zrzjUd7lFxotlCujJ4MFKBZeULraqvVF9Wg4_5Mr-EW6FgpUoTl8nLgw619zbROKes39Pa2DH" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---TEX36Y8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/30rSEw3USzJOb2Xt7gr_AH9M7dWQQBYBWOOfCwT4oVNJAowzpvDhbeKThTIpSp-zrzjUd7lFxotlCujJ4MFKBZeULraqvVF9Wg4_5Mr-EW6FgpUoTl8nLgw619zbROKes39Pa2DH" alt="How to Add Push Notifications to a Vue App" width="880" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name your app and select &lt;strong&gt;&lt;em&gt;Web&lt;/em&gt;&lt;/strong&gt; as your platform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--keyFuoBv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/54HpXgwPpt9ws3onwihy4Q0QbLNkK4v-Fm--xuMpzxkf4f-tH_d0dTRtD08442qmW7SyYfEqECR995fI_oreF0HjmeN9lVat3w9Q0_Hz0KH6zWGGdt7pwCj1IaPPz0oeIrMi7uW5" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--keyFuoBv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/54HpXgwPpt9ws3onwihy4Q0QbLNkK4v-Fm--xuMpzxkf4f-tH_d0dTRtD08442qmW7SyYfEqECR995fI_oreF0HjmeN9lVat3w9Q0_Hz0KH6zWGGdt7pwCj1IaPPz0oeIrMi7uW5" alt="How to Add Push Notifications to a Vue App" width="880" height="891"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the button labeled, &lt;strong&gt;&lt;em&gt;Next: Configure Your Platform&lt;/em&gt;&lt;/strong&gt; so you can choose how you'd like to integrate into your Vue app.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;&lt;em&gt;Web Configuration&lt;/em&gt;&lt;/strong&gt; window, select the &lt;strong&gt;&lt;em&gt;Custom Code&lt;/em&gt;&lt;/strong&gt; integration option and provide a name you'd like to use to refer to your website along with the site URL where the app is hosted. If you don't know your site URL yet, don't worry —you can set it to your localhost and update it once you deploy your Vue app.&lt;/p&gt;

&lt;p&gt;In the meantime, enable _ &lt;strong&gt;Auto Resubscribe&lt;/strong&gt; _ by moving the toggle to the right to ensure that your dev build always gets notified even when you clear your browser’s data. You'll also need to enable _ &lt;strong&gt;Local Testing&lt;/strong&gt; _ so that you can receive push notifications on your dev machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0u6keY01--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/4SoFfEjjqUmsOtSaIhZ0q-qihGn-oNmkt-5r2m2X59_QoFEnghfgOph_9Pnu9E7q4yoxjIeFbv8w-4M-64hndZVge84kYPo4rgzoWub6o3COSg82tdnPMnUxAi4WBS458Rs40lAZ" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0u6keY01--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/4SoFfEjjqUmsOtSaIhZ0q-qihGn-oNmkt-5r2m2X59_QoFEnghfgOph_9Pnu9E7q4yoxjIeFbv8w-4M-64hndZVge84kYPo4rgzoWub6o3COSg82tdnPMnUxAi4WBS458Rs40lAZ" alt="How to Add Push Notifications to a Vue App" width="880" height="1011"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the &lt;strong&gt;&lt;em&gt;Save&lt;/em&gt;&lt;/strong&gt; button to continue the configuration process.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;&lt;em&gt;4. Upload Files&lt;/em&gt;&lt;/strong&gt; section, click the &lt;strong&gt;&lt;em&gt;Download OneSignal SDK Files&lt;/em&gt;&lt;/strong&gt; button to get the service workers needed to receive push notifications or &lt;a href="https://github.com/OneSignal/OneSignal-Website-SDK/files/7585231/OneSignal-Web-SDK-HTTPS-Integration-Files.zip"&gt;download the SDK files from our Github repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yvKmUYhD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/hUL2GGxAKMNMFR6ZIGaYglYYmQs2HKbLxz-sUgBtfsnDp2xpspV85PD9hxtq0UtFPQ2Q2Z7vB2z8mSeE_83SawnGwOxx3Ebfks_TruSVigOljy4iG_wfnfPwqYaXfLH_FPW1ZrbR" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yvKmUYhD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/hUL2GGxAKMNMFR6ZIGaYglYYmQs2HKbLxz-sUgBtfsnDp2xpspV85PD9hxtq0UtFPQ2Q2Z7vB2z8mSeE_83SawnGwOxx3Ebfks_TruSVigOljy4iG_wfnfPwqYaXfLH_FPW1ZrbR" alt="How to Add Push Notifications to a Vue App" width="880" height="914"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: Configure Your Vue Application
&lt;/h2&gt;

&lt;p&gt;Now, you can follow the instructions that ship with the &lt;a href="https://www.npmjs.com/package/onesignal-vue"&gt;onesignal-vue package on npm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Install &lt;code&gt;onesignal-vue&lt;/code&gt; in your Vue project using your preferred package manager.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;yarn add onesignal-vue&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;npm install --save onesignal-vue&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Extract the service worker files from the archive you downloaded from the OneSignal dashboard and copy the files into your project’s &lt;code&gt;public&lt;/code&gt; directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initializing the OneSignal Vue Plugin
&lt;/h3&gt;

&lt;p&gt;Now you can set up the plugin by modifying &lt;code&gt;main.js&lt;/code&gt; to import the package and initialize the plugin by setting the &lt;code&gt;appId&lt;/code&gt; property to your OneSignal application identifier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Vue from "vue";
import OneSignal from "onesignal-vue";

import App from "./App.vue";

Vue.config.productionTip = false;

Vue.use(OneSignal);

new Vue({
  render: h =&amp;gt; h(App),
  beforeMount() {
    this.$OneSignal.init({
      appId: "&amp;lt;You OneSignal application identifier",
      allowLocalhostAsSecureOrigin: true,
    });
  },
}).$mount("#app");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using the Plugin on Components
&lt;/h3&gt;

&lt;p&gt;Once initialized, use the plugin on any component you like. For this tutorial, we're going to call it from the &lt;code&gt;HelloWorld.vue&lt;/code&gt; component to present a &lt;a href="https://documentation.onesignal.com/docs/slide-prompt"&gt;slide prompt&lt;/a&gt; that allows visitors to your site to opt-in to push notifications.&lt;/p&gt;

&lt;p&gt;Find the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag and set a callback for the &lt;code&gt;beforeCreate&lt;/code&gt; lifecycle hook in the exported object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import OneSignal from "onesignal-vue";
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  beforeCreate() {
    this.$OneSignal.showSlidedownPrompt();
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The integration is complete. You can test it out by starting the app using &lt;code&gt;yarn serve&lt;/code&gt; then opting into push notifications.&lt;/p&gt;

&lt;p&gt;You should have received a test push notification indicating the integration is successful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Share Your Feedback
&lt;/h2&gt;

&lt;p&gt;We'd love to know what you think of this plugin and answer any additional questions you have. To connect with us, &lt;a href="https://github.com/OneSignal/onesignal-vue/issues"&gt;create an issue on GitHub&lt;/a&gt; or ping us on the &lt;a href="https://discord.com/invite/EP7gf6Uz7G"&gt;OneSignalDevs Discord server&lt;/a&gt; to share your experience.  We appreciate any insight you can share to help us better serve Vue.js users!&lt;/p&gt;

&lt;p&gt;To stay in the loop with the latest product updates and innovations, follow the &lt;a href="https://twitter.com/onesignaldevs"&gt;OneSignal Developers Twitter&lt;/a&gt;. For additional support and dev inspiration, tap into our global developer community.&lt;/p&gt;

&lt;h3&gt;
  
  
  &amp;gt;&amp;gt; &lt;a href="https://onesignal.com/onesignal-developers"&gt;Connect with the OneSignal Developer Community&lt;/a&gt;
&lt;/h3&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using SMS as a Fallback Option for Push Notifications</title>
      <dc:creator>William Shepherd</dc:creator>
      <pubDate>Thu, 08 Jul 2021 18:24:07 +0000</pubDate>
      <link>https://dev.to/onesignal/using-sms-as-a-fallback-option-for-push-notifications-4mko</link>
      <guid>https://dev.to/onesignal/using-sms-as-a-fallback-option-for-push-notifications-4mko</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FSMS-Fallback.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FSMS-Fallback.jpg" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using SMS as a fallback communication channel for users who are not subscribed to push notifications can help you reach a larger portion of your audience while respecting their communication preferences.&lt;/p&gt;

&lt;p&gt;For example, a coffee company may use mobile push notifications to let customers know when their order is ready for pickup. To deliver a seamless customer experience for all patrons, they could set up SMS as a fallback communication channel in the event that a customer is not subscribed to push notifications. Doing so will ensure that more customers receive order pickup alerts and also creates a more inclusive and customer-centric brand experience.&lt;/p&gt;

&lt;p&gt;This 5-step guide will demonstrate how to compose OneSignal Web Push SDK with the OneSignal Rest API to enable SMS for users who are not subscribed to push notifications on your site. This example is based on the &lt;a href="https://github.com/OneSignalDevelopers/OneSignal-Nextjs-Sample" rel="noopener noreferrer"&gt;OneSignal + Next.js integration sample app&lt;/a&gt;. All of the components needed to implement this use case can be implemented using any web technology and are not limited to Next.js and React.&lt;/p&gt;

&lt;p&gt;This guide assumes that you have already configured the &lt;a href="https://documentation.onesignal.com/docs/sms-quickstart" rel="noopener noreferrer"&gt;OneSignal Twilio integration&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Check if the current user has subscribed to push notifications
&lt;/h2&gt;

&lt;p&gt;The OneSignal Web Push SDK provides an asynchronous function, known as &lt;a href="https://documentation.onesignal.com/docs/web-push-sdk#ispushnotificationsenabled" rel="noopener noreferrer"&gt;isPushNotificationEnabledCallBack&lt;/a&gt;, that returns a boolean value that describes if the current user has push notifications enabled. You’ll call this function on the client once the component mounts to the DOM.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh3.googleusercontent.com%2F9y0zeFqQV593-_6hqNnlhRN61WthGanwoRBJaxj097dYcxJEQdRygaNzdY1SRkyn3PpIsLXVm7M3nADUtK715ShItaKMeFsMmzDTZLa9dIInMDenucWpKGSZGfawsb_k4QwCLslT" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh3.googleusercontent.com%2F9y0zeFqQV593-_6hqNnlhRN61WthGanwoRBJaxj097dYcxJEQdRygaNzdY1SRkyn3PpIsLXVm7M3nADUtK715ShItaKMeFsMmzDTZLa9dIInMDenucWpKGSZGfawsb_k4QwCLslT" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Tag users who have push notifications disabled
&lt;/h2&gt;

&lt;p&gt;If the user does not have notifications turned on, you'll need to &lt;a href="https://documentation.onesignal.com/docs/web-push-sdk#sendtag" rel="noopener noreferrer"&gt;tag the user&lt;/a&gt; with some metadata to target them via our &lt;a href="https://documentation.onesignal.com/docs/segmentation" rel="noopener noreferrer"&gt;Segments&lt;/a&gt; feature.&lt;/p&gt;

&lt;p&gt;You can tag users by calling the &lt;code&gt;sendTag&lt;/code&gt; function made available by our Web Push SDK. The tag name is arbitrary, so I’m using &lt;code&gt;subscribed_to_push&lt;/code&gt; as the tag for this example. You can filter users based on these tag values — something you’ll take advantage of when creating a new audience segment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh5.googleusercontent.com%2FsEZTFA7nyUX8AXfTAWFos12zZXzXJxc_daHN9fd0jaZVNDH7BulHleYPSukdOGJXbg9oaPIPh7bX3_zuuChTlFWjKVmwmOtDjhUVRcgtC5q42HHx0On7B3pwFxx9ZND-OupJUB2y" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh5.googleusercontent.com%2FsEZTFA7nyUX8AXfTAWFos12zZXzXJxc_daHN9fd0jaZVNDH7BulHleYPSukdOGJXbg9oaPIPh7bX3_zuuChTlFWjKVmwmOtDjhUVRcgtC5q42HHx0On7B3pwFxx9ZND-OupJUB2y" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To enable an SMS notification as a fallback to push notifications, apply the &lt;code&gt;subscribed_to_push&lt;/code&gt; tag based on whether or not the user has push notifications enabled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh4.googleusercontent.com%2FZoHHc5FKPQxOFszYdbAufRRv3UHTYMJDaf_dTlnyEbHPWOYRlYo5TmWhwaN2yAwRBmv7NpsL9sBmc5msgG4dd9v8yO6c20MpWuoexDu--QKO2gmle1qSrUEPviVOx72pJtkbmNYd" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh4.googleusercontent.com%2FZoHHc5FKPQxOFszYdbAufRRv3UHTYMJDaf_dTlnyEbHPWOYRlYo5TmWhwaN2yAwRBmv7NpsL9sBmc5msgG4dd9v8yO6c20MpWuoexDu--QKO2gmle1qSrUEPviVOx72pJtkbmNYd" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Create an API endpoint that creates an SMS
&lt;/h2&gt;

&lt;p&gt;To control when a notification is sent to the user, you'll need to create an API route to request the OneSignal API to send a notification on demand. You can achieve this with the notifications endpoint on the OneSignal API.&lt;/p&gt;

&lt;p&gt;To send a text message notification, construct a request with the following shape.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    include_phone_numbers: [“+18001234567”],
    name: "Identifier for SMS",
    sms_from: "Twilio phone number",
    contents: {
    en: "English message",
    cn: "Chinese message"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because I’m using Next.js for this example, I need to add a file to &lt;code&gt;pages/api&lt;/code&gt; that exports a function that accepts an HTTP request and response object. In this example, I will set up the route to pull the phone number from the request body, but you can look up your users’ phone numbers instead.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2Fcarbon-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2Fcarbon-2.png" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Call the API endpoint that creates an SMS
&lt;/h2&gt;

&lt;p&gt;The final step to enable automatic fallback to SMS notifications is to post a request to the route responsible for creating the notification. I will make this request in the callback passed to &lt;code&gt;isPushNotificationsEnabled&lt;/code&gt; only when the user does not have push notifications enabled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh6.googleusercontent.com%2Fcl2RpmvAGnAoOFWidJgrZOFJ_YRyVP_a9g_p5AeBR2XxhMEwwLzXFGaK3SMBGRHra_SJJE0FlP4nqudFsZAIh-fwX6-RASpUxOEkC0zveReH-SssEVAxf6SWE7wr-h10D-H-b1Kw" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh6.googleusercontent.com%2Fcl2RpmvAGnAoOFWidJgrZOFJ_YRyVP_a9g_p5AeBR2XxhMEwwLzXFGaK3SMBGRHra_SJJE0FlP4nqudFsZAIh-fwX6-RASpUxOEkC0zveReH-SssEVAxf6SWE7wr-h10D-H-b1Kw" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A text message will now be sent to the numbers in the request body whenever a user navigates to a page using the OneSignal hook. The phone number is currently hard-coded for demo purposes; you may need to get the number from the app state or perform a server-side lookup for the user’s account information if your site requires that user’s login.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Push an SMS to the segment of users who are not subscribed to push notifications
&lt;/h2&gt;

&lt;p&gt;To support sending SMS notifications to all users who have opted out of push notifications, you can create a segment in your OneSignal Dashboard and filter by the tag’s value: &lt;code&gt;subscribed_to_push&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The first step to send messages to all users with push notifications disabled is to create a new segment. I’m naming the segment &lt;em&gt;Opted-out of push notifications&lt;/em&gt; and filtering by &lt;code&gt;subscribed_to_push&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt; and the device type is SMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FScreen-Shot-2021-07-01-at-12.21.49-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FScreen-Shot-2021-07-01-at-12.21.49-PM.png" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sending an SMS notification to all users in the segment requires creating a message to send and selecting this new segment as the target.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FScreen-Shot-2021-07-01-at-12.22.45-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FScreen-Shot-2021-07-01-at-12.22.45-PM.png" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FScreen-Shot-2021-07-01-at-12.24.16-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F07%2FScreen-Shot-2021-07-01-at-12.24.16-PM.png" alt="Using SMS as a Fallback Option for Push Notifications"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click send, the text message will be sent to everyone in the segment, completing the workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Updates on the Horizon
&lt;/h2&gt;

&lt;p&gt;We are in the process of developing a new push-to-SMS retargeting functionality that will enhance our SMS fallback delivery capabilities. If you're interested in trialing this feature and providing feedback to inform our product roadmap, click the link below to learn more about participating in our Beta Program.&lt;/p&gt;

&lt;h3&gt;
  
  
  &amp;gt;&amp;gt; &lt;a href="https://onesignal.com/beta-program" rel="noopener noreferrer"&gt;Learn More About the OneSignal Beta Program&lt;/a&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Additional Support
&lt;/h3&gt;

&lt;p&gt;Have questions or need some support getting started? We're here to help. Reach out to us at &lt;strong&gt;&lt;a href="mailto:support@onesignal.com"&gt;support@onesignal.com&lt;/a&gt;&lt;/strong&gt; or &lt;a href="https://app.onesignal.com/login" rel="noopener noreferrer"&gt;login to your OneSignal account&lt;/a&gt; and send us a message from your dashboard.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>nextjs</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to add push notifications into a Next.js App</title>
      <dc:creator>William Shepherd</dc:creator>
      <pubDate>Wed, 16 Jun 2021 19:53:12 +0000</pubDate>
      <link>https://dev.to/onesignal/how-to-integrate-onesignal-into-a-next-js-app-1dmg</link>
      <guid>https://dev.to/onesignal/how-to-integrate-onesignal-into-a-next-js-app-1dmg</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fonesignal-nextapp-integration.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fonesignal-nextapp-integration.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Web push notifications are a versatile channel that can be used to enhance your UX, re-engage users, promote new products or features, ignite engagement, drive revenue, and &lt;a href="https://onesignal.com/blog/easy-strategies-to-increase-app-retention/" rel="noopener noreferrer"&gt;increase user retention&lt;/a&gt;. Compared to traditional channels such as email, &lt;a href="https://onesignal.com/webpush" rel="noopener noreferrer"&gt;web push notifications&lt;/a&gt; capture user's attention more quickly and can drive immediate engagement, which is ideal for time-sensitive content. The best part? You can add this powerful communication channel to your Next app for free.&lt;/p&gt;

&lt;p&gt;In this guide, I'll show you how to integrate with OneSignal in order to add web push notifications to a React app built using the &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; framework. This article is written with the assumption that you already know a little bit about Next.js — my focus will be on executing the OneSignal integration. In an effort to make this guide as useful to as many people as possible, I will start from scratch and cover the Next.js app setup process as well as the OneSignal integration and messaging process.&lt;/p&gt;

&lt;p&gt;At the time this guide was written, I had the following dependencies installed on my system: &lt;strong&gt;macOS 11.3.1&lt;/strong&gt; , &lt;a href="https://github.com/nodejs/node/releases/tag/v14.15.1" rel="noopener noreferrer"&gt;&lt;strong&gt;Node v14.15.1&lt;/strong&gt;&lt;/a&gt;, &lt;a href="https://github.com/yarnpkg/yarn/releases/tag/v1.22.10" rel="noopener noreferrer"&gt;&lt;strong&gt;Yarn 1.22.10&lt;/strong&gt;&lt;/a&gt;, &lt;a href="https://github.com/vercel/next.js/releases/tag/v10.2.0" rel="noopener noreferrer"&gt;&lt;strong&gt;Next.js v10.2.0&lt;/strong&gt;&lt;/a&gt; (latest at the time of writing). You should be able to follow along so long as you have &lt;strong&gt;Node version 14 or later&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guide Overview
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Creating Your Next App&lt;/li&gt;
&lt;li&gt;Getting the OneSignal SDK&lt;/li&gt;
&lt;li&gt;Adding the OneSignal SDK Script to Your App&lt;/li&gt;
&lt;li&gt;
Part 1: Initializing the OneSignal SDK &lt;/li&gt;
&lt;li&gt;Configuring Your Next App in OneSignal&lt;/li&gt;
&lt;li&gt;Part 2: Initializing the OneSignal SDK&lt;/li&gt;
&lt;li&gt;Sending &amp;amp; Receiving Notifications&lt;/li&gt;
&lt;li&gt;Additional Support &amp;amp; Helpful Resources&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don't yet have a OneSignal account, &lt;a href="https://app.onesignal.com/signup" rel="noopener noreferrer"&gt;create a free account&lt;/a&gt; before you begin this guide. Don't worry about configuring your account just yet — simply create your login and password and I'll walk you through the platform setup and initialization process later on in this guide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Your Next App
&lt;/h2&gt;

&lt;p&gt;Execute &lt;code&gt;yarn create next-app&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

╭─iamwillshepherd@ares ~/code/onesignal-nextjs ‹main›
╰─$ yarn create next-app .
yarn create v1.22.10
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...

success Installed "create-next-app@10.2.0" with binaries:
      - create-next-app
[##] 2/2Creating a new Next.js app in /Users/iamwillshepherd/code/onesignal-nextjs.

Installing react, react-dom, and next using yarn...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When the command completes, you should see console output similar to this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Success! Created onesignal-nexgtjs at /Users/iamwillshepherd/code/onesignal-nextjs
Inside that directory, you can run several commands:

  yarn dev
    Starts the development server.

  yarn build
    Builds the app for production.

  yarn start
    Runs the built app in production mode.

We suggest that you begin by typing:

  cd /Users/iamwillshepherd/code/onesignal-nextjs
  yarn dev

✨ Done in 4.75s.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can check out the excellent &lt;a href="https://nextjs.org/docs" rel="noopener noreferrer"&gt;Next documentation&lt;/a&gt; to learn what this (☝🏾) command does.&lt;/p&gt;

&lt;p&gt;Execute &lt;code&gt;yarn dev&lt;/code&gt; to verify the app works as expected.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

╭─iamwillshepherd@ares ~/code/onesignal-nextjs ‹main*›
╰─$ yarn dev
yarn run v1.22.10
$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - Using webpack 5. Reason: no next.config.js https://nextjs.org/docs/messages/webpack5
event - compiled successfully
event - build page: /next/dist/pages/_error
wait - compiling...
event - compiled successfully


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Navigate to the URL returned in the output with your browser of choice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-17_at_12.23.19_PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-17_at_12.23.19_PM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;The expected result when navigating to URL in run output.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the OneSignal SDK
&lt;/h2&gt;

&lt;p&gt;Our platform allows you to integrate with dozens of third parties. Because I'm integrating OneSignal into a React app, I must manually add the SDK service workers to the app. For instructions on how to do so, check out our &lt;a href="https://documentation.onesignal.com/docs/web-push-custom-code-setup" rel="noopener noreferrer"&gt;Custom Code Setup Documentation&lt;/a&gt;. To get started, download the OneSignal SDK archive &lt;a href="https://documentation.onesignal.com/docs/web-push-custom-code-setup#step-3-upload-onesignal-sdk" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Unzip the archive contents into your project into the &lt;code&gt;public&lt;/code&gt; directory of your Next app. The OneSignal Web SDK directory contains service workers that do the heavy lifting of handling notifications. These service workers must be publicly accessible, so we use Next's &lt;a href="https://nextjs.org/docs/basic-features/static-file-serving" rel="noopener noreferrer"&gt;static file servicing&lt;/a&gt; to achieve this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-17_at_2.27.14_PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-17_at_2.27.14_PM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ignore the contents of &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;__MACOSX&lt;/code&gt;. Move all JavaScript files from &lt;code&gt;OneSignal-Web-SDK/&lt;/code&gt; to &lt;code&gt;public/&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

╭─iamwillshepherd@ares ~/code/onesignal-nextjs/public ‹main*›
╰─$ mv OneSignal-Web-SDK-HTTPS-Integration-Files/*.js .


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Confirm that the files have been moved and then remove the highlighted files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-17_at_3.44.51_PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-17_at_3.44.51_PM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, cleanup the &lt;code&gt;public&lt;/code&gt; directory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

╭─iamwillshepherd@ares ~/code/onesignal-nextjs/public ‹main*›
╰─$ rm -rf __MACOSX OneSignal-Web-*


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Adding the OneSignal SDK Script to Your App
&lt;/h2&gt;

&lt;p&gt;The OneSignal SDK script must be loaded to make use of the two service workers. Add the OneSignalSDK script under the &lt;code&gt;Head&lt;/code&gt; component in &lt;code&gt;pages/index.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following script tag&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;OneSignal + Next.js&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt;
    &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"description"&lt;/span&gt;
    &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"Integrating OneSignal with a Next.js app."&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"icon"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/favicon.ico"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script
    &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.onesignal.com/sdks/OneSignalSDK.js"&lt;/span&gt;
    &lt;span class="na"&gt;async=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;At this point, you have completed the majority of the web app setup process. The last thing you need to do is initialize an OneSignalSDK instance  &lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Your Next App in OneSignal
&lt;/h2&gt;

&lt;p&gt;Custom code integrations require a bit of JavaScript code to initialize OneSignal. I'm going to focus on HTTP initialization because more people will be able to follow along. HTTPS initialization is very similar to what I'm covering here, so this guide will still be helpful to you.&lt;/p&gt;

&lt;p&gt;OneSignal needs a unique key called &lt;code&gt;appId&lt;/code&gt; to initialize the SDK. You can obtain this key by logging into your &lt;a href="https://app.onesignal.com/login" rel="noopener noreferrer"&gt;OneSignal account&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If this is the first time you've logged into your OneSignal account, you will be presented with a welcome page asking you to configure the platform. Create a new app for the Web platform named &lt;strong&gt;&lt;em&gt;OneSignal Next.js&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;by&lt;/em&gt; filling out the form and selecting _ &lt;strong&gt;Web&lt;/strong&gt; _ as your platform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-19_at_11.01.43_AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-19_at_11.01.43_AM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Configuring new OneSignal app for the Web platform.&lt;/p&gt;

&lt;p&gt;If you've already configured your OneSignal for other apps, you can create a new app by selecting &lt;strong&gt;&lt;em&gt;+New App&lt;/em&gt;&lt;/strong&gt; from the apps drop-down menu in your dashboard, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2F2021-05-19_11.22.17.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2F2021-05-19_11.22.17.gif" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Select "New App" from the app dropdown menu in your OneSignal dashboard.&lt;/p&gt;

&lt;p&gt;Once you've finished filling out the form, click &lt;strong&gt;&lt;em&gt;Next: Configure Your Platform&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Under &lt;strong&gt;&lt;em&gt;1. Choose Integration&lt;/em&gt;&lt;/strong&gt; , select the _ &lt;strong&gt;Custom Code&lt;/strong&gt; _ option and fill out the remaining form fields in the &lt;strong&gt;&lt;em&gt;2.&lt;/em&gt;&lt;/strong&gt; _ &lt;strong&gt;Site Setup&lt;/strong&gt; _ section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-19_at_11.19.58_AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-19_at_11.19.58_AM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Configuring a custom code web integration in OneSignal.&lt;/p&gt;

&lt;p&gt;Note that I'm using &lt;code&gt;http://localhost:3000&lt;/code&gt; for my site URL because my dev server serves the site there. Once you're ready to deploy your site, you would change this URL to point to your domain. You'll also want to double-check that you enable the &lt;em&gt;Local Testing&lt;/em&gt; option, allowing the integration to work in a development environment.&lt;/p&gt;

&lt;p&gt;Lastly, click the &lt;strong&gt;&lt;em&gt;Save&lt;/em&gt;&lt;/strong&gt; to complete the application setup process.&lt;/p&gt;

&lt;p&gt;The final page of the app creation process reveals &lt;code&gt;appId&lt;/code&gt; in the second script. Copy the content of the second script.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2F5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2F5.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Script needed to initialize OneSignal SDK&lt;/p&gt;

&lt;h2&gt;
  
  
  Initializing the OneSignal SDK
&lt;/h2&gt;

&lt;p&gt;Next.js uses React to render the app, so I have to handle the initialization of the SDK in a way that works with the framework. React provides a mechanism to perform side effects on page load: &lt;code&gt;useEffect&lt;/code&gt; (read the &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;doc&lt;/a&gt; to learn more). This hook allows code to execute when the page is mounted, which is necessary to init OneSignal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OneSignal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OneSignal&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="nx"&gt;OneSignal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;OneSignal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;appId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b40b7cc7-13dc-4662-8b48-efa668f9b72a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;notifyButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;enable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;

            &lt;span class="na"&gt;allowLocalhostAsSecureOrigin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OneSignal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- run this effect once on mount&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
This hook should be located in &lt;code&gt;_app.js&lt;/code&gt;



&lt;p&gt;Reloading the app should reveal a new UI element (a circular red button with a bell at the center) at the bottom-right corner of the browser viewport.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FOneSignal---Next.js-app.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FOneSignal---Next.js-app.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;The bell button is how site visitors can subscribe to receive notifications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending &amp;amp; Receiving Notifications
&lt;/h2&gt;

&lt;p&gt;Now that you've finished the initialization process, you can create and send your first web push notification directly from your OneSignal dashboard. To test out this new messaging functionality, you'll first need to subscribe to receive notifications in your browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subscribing to Notifications
&lt;/h3&gt;

&lt;p&gt;Subscribe to notifications by clicking on the red button at the bottom right of your screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-19_at_2.35.11_PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-19_at_2.35.11_PM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Subscribe to notifications by clicking the button at the bottom right corner of your screen.&lt;/p&gt;

&lt;p&gt;A dialogue box will appear at the top of your browser window asking you whether you would like to be shown notifications. Click _ &lt;strong&gt;Allow&lt;/strong&gt; _ to subscribe.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-20_at_10.04.51_AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-20_at_10.04.51_AM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Click "allow" to subscribe to notifications for your Next.js app.&lt;/p&gt;

&lt;p&gt;To confirm your subscription, navigate to your &lt;a href="https://app.onesignal.com/" rel="noopener noreferrer"&gt;OneSignal account&lt;/a&gt; and select the _ &lt;strong&gt;OneSignal Nextjs&lt;/strong&gt; _ app. Click the &lt;strong&gt;&lt;em&gt;Audience&lt;/em&gt;&lt;/strong&gt; tab from the header menu to view subscribed users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fonesignal-dashboard-audience-subscribed-users-screenshot.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fonesignal-dashboard-audience-subscribed-users-screenshot.gif" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;View your subscriber list by selecting your Next.js app and navigating to the audience tab.&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;&lt;em&gt;All Users&lt;/em&gt;&lt;/strong&gt; from the sub-navigation menu to see a list of all subscribed users for this app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fonesignal-dashboard-subscribed-users-page.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fonesignal-dashboard-subscribed-users-page.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;The "All Users" tab shows a list of your current subscribed users.&lt;/p&gt;

&lt;p&gt;At this point, you should be your only subscribed user 😅.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building Your Message
&lt;/h3&gt;

&lt;p&gt;Now that you have a subscriber, you can use OneSignal to push a web notification. To build a new messaging campaign, selecting _ &lt;strong&gt;Messages&lt;/strong&gt; _ from the main navigation menu and click the blue &lt;strong&gt;&lt;em&gt;+&lt;/em&gt;&lt;/strong&gt; _ &lt;strong&gt;New Push&lt;/strong&gt; _ button on the top right corner of the screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen-Shot-2021-06-11-at-5.23.05-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen-Shot-2021-06-11-at-5.23.05-PM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Create a new web push notification in your OneSignal dashboard.&lt;/p&gt;

&lt;p&gt;Fill out the form with your message content and message delivery preferences. You will see a preview of how your notification will look on the right side of your screen.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-20_at_10.47.17_AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen_Shot_2021-05-20_at_10.47.17_AM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Build your push notification and preview how it will look.&lt;/p&gt;

&lt;p&gt;For testing purposes, our notification is configured to send immediately. The notification preview shows what the message will look like for a Mac user who has subscribed to the site using Chrome. Click the &lt;strong&gt;&lt;em&gt;Web: macOS button&lt;/em&gt;&lt;/strong&gt; located below the preview image to see how your notification will look on different platforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen-Shot-2021-06-11-at-5.08.30-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2FScreen-Shot-2021-06-11-at-5.08.30-PM.png" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;OneSignal's notification preview tool.&lt;/p&gt;

&lt;p&gt;It's important to check the preview for all platforms before pushing a notification so that you can make any necessary formatting adjustments. Because this is a demo, I will push this notification even though the layout is broken for the Android web platform.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sending Your Notification
&lt;/h3&gt;

&lt;p&gt;When you are finished previewing your notification, click &lt;strong&gt;&lt;em&gt;Confirm Message&lt;/em&gt;&lt;/strong&gt; to review your selections and click _ &lt;strong&gt;Send Message&lt;/strong&gt; _ to send it to your subscribers (a.k.a yourself).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Freview-and-send-your-web-push-notification-onesignal-1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Freview-and-send-your-web-push-notification-onesignal-1.gif" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;Review and send your message.&lt;/p&gt;

&lt;p&gt;You should see your notification appear on your desktop. If you entered a URL into the &lt;strong&gt;&lt;em&gt;Launch URL&lt;/em&gt;&lt;/strong&gt; form field of the message builder tool, then clicking on your notification should open a browser window with your designated URL destination. In my demo example, I entered the &lt;a href="https://github.com/OneSignal/OneSignal-Nextjs-Sample" rel="noopener noreferrer"&gt;GitHub address&lt;/a&gt; for this integration guide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fview-web-notification-and-take-action-onesignal.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fonesignal.com%2Fblog%2Fcontent%2Fimages%2F2021%2F06%2Fview-web-notification-and-take-action-onesignal.gif" alt="How to Integrate OneSignal into a Next.js App"&gt;&lt;/a&gt;View your web push notification and click on it to trigger your specified action.&lt;/p&gt;

&lt;p&gt;Congratulations! You've successfully completed the Next.js and OneSignal integration process and sent your first web push notification.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Support &amp;amp; Helpful Resources
&lt;/h2&gt;

&lt;p&gt;To learn more about web push notifications and explore customization options, check out our &lt;a href="https://documentation.onesignal.com/docs/web-push-sdk" rel="noopener noreferrer"&gt;Web Push SDK documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are using web notifications in a creative way to improve your Next app UX, we want to hear about it! Tag us in a Tweet &lt;strong&gt;&lt;em&gt;@onesignal&lt;/em&gt;&lt;/strong&gt; to share your use cases.&lt;/p&gt;

&lt;p&gt;Still have questions or want some help getting started? Email us at &lt;strong&gt;&lt;em&gt;&lt;a href="mailto:support@onesignal.com"&gt;support@onesignal.com&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
