> ## Documentation Index
> Fetch the complete documentation index at: https://documentation.onesignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# v3-4 SDK SMS SDK Methods

> Collect SMS Numbers through your app or website.

<Warning>
  The methods below require the OneSignal SDK versions 3 & 4.

  It is recommended to upgrade to our latest version 5 SDKs for User Model APIs.

  See [Update to User Model](/docs/en/user-model-migration-guide) for migration steps.
</Warning>

If you have not done so, we always recommend updating the OneSignal SDK to the latest version to get access to the latest features:

* [Web Push Quickstart](/docs/en/web-push-setup)
* [Mobile Push Quickstart](/docs/en/mobile-sdk-setup)

<Warning>
  SMS and Push subscribers will have separate OneSignal Subscription IDs (user records). This is to manage the case where a user opts-out of one you can still send messages to the other.
</Warning>

<Warning>
  If there are networking issues, functions may take 30+ seconds to return. In the code examples below we provide callbacks to track when these return.

  Make sure to keep functions from blocking the user interface, otherwise your app may appear unresponsive to the user.
</Warning>

# Setting the User's SMS Number

## `setSMSNumber` Method

The `setSMSNumber` method allows you to set the user's SMS number with the OneSignal SDK. [We offer several overloaded versions of this method.](/v9.0/docs/sdk-reference#sms)

<CodeGroup>
  ```javascript Web-JavaScript theme={null}
  OneSignal.push(function() {
    OneSignal.setSMSNumber("+11234567890");
  });
  ```

  ```java java theme={null}
  OneSignal.setSMSNumber("+11234567890");
  ```

  ```kotlin kotlin theme={null}
  OneSignal.setSMSNumber("+11234567890")
  ```

  ```swift Swift theme={null}
  OneSignal.setSMSNumber("+11234567890")
  ```

  ```objectivec objectivec theme={null}
  [OneSignal setSMSNumber:@"+11234567890"];
  ```

  ```javascript React Native theme={null}
  OneSignal.setSMSNumber("+11234567890");
  ```

  ```javascript Flutter theme={null}
  OneSignal.shared.setSMSNumber(smsNumber: "+11234567890")
  ```

  ```javascript Cordova/Ionic theme={null}
  // Ionic 5 Capacitor may need to use (window as any).plugins.OneSignal
  window.plugins.OneSignal.setSMSNumber("+11234567890");
  ```

  ```csharp Unity(C#) theme={null}
  OneSignal.Default.SetSMSNumber("+12345556789");

  var result = await OneSignal.Default.SetSMSNumber("+12345556789");
  if (result) {
      Debug.Log("success");
  }
  else {
      Debug.Log("error");
  }
  ```
</CodeGroup>

If you have a backend server, we strongly recommend using [Identity Verification](/v9.0/docs/identity-verification) with your users. Your backend can generate an ***SMS authentication token*** and send it to your app or website.

Note that the SMS number is required to be in the E.164 format. See [SMS setup](/docs/en/sms-setup#what-is-e164-format) for more details.

# Logout Number

## `logoutSMSNumber` Method

If your app or website implements logout functionality, you can call `logoutSMSNumber` to dissociate the SMS from the device:

<CodeGroup>
  ```javascript Web-JavaScript theme={null}
  OneSignal.push(function() {
    OneSignal.logoutSMS();
  });
  ```

  ```java java theme={null}
  OneSignal.logoutSMSNumber();
  ```

  ```swift Swift theme={null}
  OneSignal.logoutSMSNumber()
  ```

  ```objectivec objectivec theme={null}
  [OneSignal logoutSMSNumber];
  ```

  ```javascript React Native theme={null}
  OneSignal.logoutSMSNumber();
  ```

  ```javascript Flutter theme={null}
  OneSignal.shared.logoutSMSNumber()
  ```

  ```javascript Cordova/Ionic theme={null}
  // Ionic 5 Capacitor may need to use (window as any).plugins.OneSignal
  window.plugins.OneSignal.logoutSMSNumber();
  ```

  ```csharp Unity(C#) theme={null}
  OneSignal.Default.LogOutSMS();

  var result = await OneSignal.Default.LogOutSMS();
  if (result) {
      Debug.Log("success");
  }
  else {
      Debug.Log("error");
  }
  ```
</CodeGroup>

# Listening for subscription changes

The SMS Subscription Observer tracks changes to SMS subscriptions (ie. the user sets their SMS number or logs out). In order to subscribe to SMS subscription changes you can implement the following:

| Event Object Property | Type                         |
| --------------------- | ---------------------------- |
| `sms`                 | `string` e.g: "+12149874829" |

<CodeGroup>
  ```java java theme={null}
  OneSignal.addSMSSubscriptionObserver(stateChanges -> {
    	// Work with state changes          
    });
  ```

  ```objectivec objectivec theme={null}
  [OneSignal addSMSSubscriptionObserver:self];
  ```

  ```swift Swift theme={null}
  OneSignal.add(self as OSSMSSubscriptionObserver)
  ```

  ```javascript React Native theme={null}
  OneSignal.addSMSSubscriptionObserver((event) => {
    console.log("OneSignal: sms subscription changed: ", event);
  });
  ```

  ```javascript Flutter theme={null}
  OneSignal.shared.setSMSSubscriptionObserver((OSSMSSubscriptionStateChanges changes) {
  });
  ```

  ```csharp Unity(C#) theme={null}
  OneSignal.Default.SMSSubscriptionStateChanged += (current, previous) => {
      var smsSubscribed = current.isSubscribed;
  };
  ```

  ```javascript javascript theme={null}
  OneSignal.on("smsSubscriptionChanged", event => {
     const { sms } = event;
     console.log("sms: ", sms);
  });
  ```
</CodeGroup>

Now, whenever the sms subscription changes, this method will be called:

<CodeGroup>
  ```java java theme={null}
  OSSMSSubscriptionObserver subscriptionObserver = new OSSMSSubscriptionObserver() {
     @Override
     public void  public void onSMSSubscriptionChanged(OSSMSSubscriptionStateChanges stateChanges) {
     }
  };
  ```

  ```objectivec objectivec theme={null}
  -(void)onOSSMSSubscriptionChanged:(OSSMSSubscriptionStateChanges *)stateChanges {
      
  }
  ```

  ```swift Swift theme={null}
  func onOSSMSSubscriptionChanged(_ stateChanges: OSSMSSubscriptionStateChanges!) { 
      
  }
  ```
</CodeGroup>

## `getSMSId` Method

Get the OneSignal Subscription ID associated with the SMS Phone Number Record.

<CodeGroup>
  ```javascript Web-JavaScript theme={null}
  OneSignal.push(function() {
    OneSignal.getSMSId();
  });
  ```

  ```csharp Unity(C#) theme={null}
  var smsState  = OneSignal.Default.SMSSubscriptionState;
  var smsUserId = smsState.smsUserId;
  var smsNumber = smsState.smsNumber;
  ```
</CodeGroup>

***
