Using SMS as a Fallback Option for Push Notifications
Tags: javascript, react, nextjs, webdev
Published: 2021-07-12 18:53:54

---
title: Using SMS as a Fallback Option for Push Notifications
published: true
date: 2021-07-08 18:24:07 UTC
tags: javascript, react, nextjs, webdev
canonical_url: https://onesignal.com/blog/using-sms-as-a-fallback-option-for-unsubscribed-push-users/
---

 ![Using SMS as a Fallback Option for Push Notifications](https://onesignal.com/blog/content/images/2021/07/SMS-Fallback.jpg)

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.

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.

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 [OneSignal + Next.js integration sample app](https://github.com/OneSignalDevelopers/OneSignal-Nextjs-Sample). 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.

This guide assumes that you have already configured the [OneSignal Twilio integration](https://documentation.onesignal.com/docs/sms-quickstart).

## 1. Check if the current user has subscribed to push notifications

The OneSignal Web Push SDK provides an asynchronous function, known as [isPushNotificationEnabledCallBack](https://documentation.onesignal.com/docs/web-push-sdk#ispushnotificationsenabled), 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.

![Using SMS as a Fallback Option for Push Notifications](https://lh3.googleusercontent.com/9y0zeFqQV593-_6hqNnlhRN61WthGanwoRBJaxj097dYcxJEQdRygaNzdY1SRkyn3PpIsLXVm7M3nADUtK715ShItaKMeFsMmzDTZLa9dIInMDenucWpKGSZGfawsb_k4QwCLslT)

## 2. Tag users who have push notifications disabled

If the user does not have notifications turned on, you'll need to [tag the user](https://documentation.onesignal.com/docs/web-push-sdk#sendtag) with some metadata to target them via our [Segments](https://documentation.onesignal.com/docs/segmentation) feature.

You can tag users by calling the `sendTag` function made available by our Web Push SDK. The tag name is arbitrary, so I’m using `subscribed_to_push` 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.

![Using SMS as a Fallback Option for Push Notifications](https://lh5.googleusercontent.com/sEZTFA7nyUX8AXfTAWFos12zZXzXJxc_daHN9fd0jaZVNDH7BulHleYPSukdOGJXbg9oaPIPh7bX3_zuuChTlFWjKVmwmOtDjhUVRcgtC5q42HHx0On7B3pwFxx9ZND-OupJUB2y)

To enable an SMS notification as a fallback to push notifications, apply the `subscribed_to_push` tag based on whether or not the user has push notifications enabled.

![Using SMS as a Fallback Option for Push Notifications](https://lh4.googleusercontent.com/ZoHHc5FKPQxOFszYdbAufRRv3UHTYMJDaf_dTlnyEbHPWOYRlYo5TmWhwaN2yAwRBmv7NpsL9sBmc5msgG4dd9v8yO6c20MpWuoexDu--QKO2gmle1qSrUEPviVOx72pJtkbmNYd)

## 3. Create an API endpoint that creates an SMS 

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.

To send a text message notification, construct a request with the following shape.

```
{
    include_phone_numbers: [“+18001234567”],
    name: "Identifier for SMS",
    sms_from: "Twilio phone number",
    contents: {
    en: "English message",
    cn: "Chinese message"
}
```

Because I’m using Next.js for this example, I need to add a file to `pages/api` 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.

![Using SMS as a Fallback Option for Push Notifications](https://onesignal.com/blog/content/images/2021/07/carbon-2.png)

## 4. Call the API endpoint that creates an SMS

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 `isPushNotificationsEnabled` only when the user does not have push notifications enabled.

![Using SMS as a Fallback Option for Push Notifications](https://lh6.googleusercontent.com/cl2RpmvAGnAoOFWidJgrZOFJ_YRyVP_a9g_p5AeBR2XxhMEwwLzXFGaK3SMBGRHra_SJJE0FlP4nqudFsZAIh-fwX6-RASpUxOEkC0zveReH-SssEVAxf6SWE7wr-h10D-H-b1Kw)

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.

## 5. Push an SMS to the segment of users who are not subscribed to push notifications

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: `subscribed_to_push`.

The first step to send messages to all users with push notifications disabled is to create a new segment. I’m naming the segment _Opted-out of push notifications_ and filtering by `subscribed_to_push` is `false` and the device type is SMS.

![Using SMS as a Fallback Option for Push Notifications](https://onesignal.com/blog/content/images/2021/07/Screen-Shot-2021-07-01-at-12.21.49-PM.png)

Sending an SMS notification to all users in the segment requires creating a message to send and selecting this new segment as the target.

![Using SMS as a Fallback Option for Push Notifications](https://onesignal.com/blog/content/images/2021/07/Screen-Shot-2021-07-01-at-12.22.45-PM.png)

![Using SMS as a Fallback Option for Push Notifications](https://onesignal.com/blog/content/images/2021/07/Screen-Shot-2021-07-01-at-12.24.16-PM.png)

When you click send, the text message will be sent to everyone in the segment, completing the workflow.

## Other Updates on the Horizon

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.

### >> [Learn More About the OneSignal Beta Program](https://onesignal.com/beta-program)

### Additional Support

Have questions or need some support getting started? We're here to help. Reach out to us at **support@onesignal.com** or [login to your OneSignal account](https://app.onesignal.com/login) and send us a message from your dashboard.