22

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.

Lane Rettig
  • 6,640
  • 5
  • 42
  • 51
  • 1
    Possible duplicate of [SMS URL on Android](http://stackoverflow.com/questions/4787905/sms-url-on-android) – Lane Rettig Mar 22 '16 at 20:20
  • 1
    Not quite a duplicate, since the question wants a cross-platform solution and in fact iOS is slightly different. – Rob Hogan Dec 15 '17 at 15:03
  • If you want the user to select from contacts, refer my [Answer](https://stackoverflow.com/questions/43781338/react-native-ios-open-message-app-with-default-text/53615008#53615008) – Akshay Soam Dec 04 '18 at 14:23

7 Answers7

23

Have you considered and tried sms:number?body=yourMessage?

You can read up on it in RFC 5724.

Community
  • 1
  • 1
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
16

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" ? "&" : "?";
}
Ronny Roktel
  • 354
  • 2
  • 9
6

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
}
Luan Eduardo
  • 406
  • 4
  • 4
  • How can I use this hook? –  Feb 26 '21 at 07:44
  • 1
    1 - Create a file for the hook. It could be called "useSendSMS" 2 - Import in the component that you want to use. 3 - Execute the hook. It will return an async function that receives a phone number and a message. Since it's an async function make sure to wrap the execution within a try catch block – Luan Eduardo Feb 26 '21 at 13:47
  • This link may help you: https://reactjs.org/docs/hooks-custom.html – Luan Eduardo Feb 26 '21 at 13:50
  • if I pass phonenumber with the country code then on iPad send button is not active, but if I pass phone-number without country code then send button is active on iPad – Neerav Shah Jul 12 '23 at 07:47
4

OR you can simply do

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

Linking.openURL(url);
abdul jalil
  • 136
  • 6
4

This should work on both Platforms

 const operator = Platform.select({ios: '&', android: '?'});
 Linking.openURL(`sms:${operator}body=${options.title}`);
Hamza Waleed
  • 1,334
  • 10
  • 12
1

Pretty sure this is not React Native-specific, and is just about linking on Android. Take a look at: SMS URL on Android

Community
  • 1
  • 1
Lane Rettig
  • 6,640
  • 5
  • 42
  • 51
0

You can have a look at the react-packages react-native-communications(https://github.com/anarchicknight/react-native-communications). You can probably integrate the entire package or take parts of it

Nishanth Shankar
  • 1,966
  • 1
  • 14
  • 19