React Native Linking SMS

Viewed 22388

Now that RN supports Linking cross-platform, I am wondering how to send an SMS with a preset message. Per the docs (https://facebook.github.io/react-native/docs/linking.html#content):

Try to open the given url with any of the installed apps. You can use other URLs, like a location (e.g. "geo:37.484847,-122.148386"), a contact, or any other URL that can be opened with the installed apps.

My question is, what URI scheme do I follow to open an SMS with a predefined message? Is it cross-platform?

Thank you.

7 Answers

there's a difference between the platforms with the "body" divider you can use the following code to make it work:

function openUrl(url: string): Promise<any> {
  return Linking.openURL(url);
}
export function openSmsUrl(phone: string, body: string): Promise<any> {
  return openUrl(`sms:${phone}${getSMSDivider()}body=${body}`);
}
function getSMSDivider(): string {
  return Platform.OS === "ios" ? "&" : "?";
}

I created a hook to send SMS messages:

import { useCallback } from 'react'
import { Linking, Platform } from 'react-native'

export default () => {
  const onSendSMSMessage = useCallback(async (phoneNumber, message) => {
    const separator = Platform.OS === 'ios' ? '&' : '?'
    const url = `sms:${phoneNumber}${separator}body=${message}`
    await Linking.openURL(url)
  }, [])

  return onSendSMSMessage
}

OR you can simply do

url = `sms:${context.provider.state.driverPhoneNo}${Platform.OS === "ios" ? "&" : "?"}body=${""}`

Linking.openURL(url);

This should work on both Platforms

 const operator = Platform.select({ios: '&', android: '?'});
 Linking.openURL(`sms:${operator}body=${options.title}`);
Related