1# SMS Service Development 2 3## When to Use 4 5The Short Messaging Service (SMS) module provides basic SMS management functions. You can create and send SMS messages, and obtain the ID of the default SIM card used to send and receive SMS messages. Besides, you can obtain and set the SMSC address, and check whether the current device can send and receive SMS messages. 6 7## Basic Concepts 8 9- SMS 10 11 A service capable of SMS message storage and forwarding. It enables mobile phones to send and receive SMS messages. The content of the SMS message can be text, digits, or binary non-text data. The information about the sender is stored in the Short Message Service Center (SMSC) and forwarded to the recipient. 12 13- SMSC 14 15 An entity that relays, stores, or forwards SMS messages between base stations and mobile devices. It uses the GSM 03.40 protocol for sending SMS messages to or receiving SMS messages from mobile phones. 16 17- PDU 18 19 Protocol data unit, which uses the following encoding schemes to send and receive SMS messages: 7-bit, 8-bit, and UCS-2. 7-bit encoding is used to send common ASCII characters, 8-bit encoding to send data messages, and UCS-2 encoding to send Unicode characters. 20 21## Constraints 22 231. The SMS service is available only on standard-system devices. 242. An available SIM card must be present on the device, and the permission to send SMS messages must be granted. 25 26 27## Available APIs 28 29> **NOTE** 30> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [API Reference](../reference/apis-telephony-kit/js-apis-sms.md). 31 32| Name | Description | 33| ------------------------------------------------------------ | ------------------------------------------------------- | 34| sendShortMessage(options: SendMessageOptions, callback: AsyncCallback\<void\>): void | Sends text or data SMS messages. You need to request for the **ohos.permission.SEND_MESSAGES** permission, which is available only for system applications. | 35| createMessage(pdu: Array\<number\>, specification: string, callback: AsyncCallback\<ShortMessage\>): void | Creates an SMS message instance based on the PDU and the specified SMS protocol.| 36| getDefaultSmsSlotId(callback: AsyncCallback\<number\>): void | Obtains the ID of the default SIM card used to send SMS messages. | 37| <!--DelRow-->setSmscAddr(slotId: number, smscAddr: string, callback: AsyncCallback\<void\>): void | Sets the SMSC address based on the specified slot ID. | 38| <!--DelRow-->getSmscAddr(slotId: number, callback: AsyncCallback\<string>): void | Obtains the SMSC address based on the specified slot ID. | 39 40<!--Del--> 41## How to Develop (for System Applications Only) 42 431. Declare the required permission: 44 - To send SMS messages, call the **sendShortMessage** API and declare the **ohos.permission.SEND\_MESSAGES** permission. The permission is of the **system\_basic** level. 45 - To set the SMSC address, call the** setSmscAddr** API and declare the **ohos.permission.SET\_TELEPHONY\_STATE** permission. The permission is of the **system\_basic** level. 46 - To obtain the SMSC address, call the** getSmscAddr** API and declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission. The permission is of the **system\_basic** level. 47 Before requesting the permission, ensure that the [basic principles for using permissions](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Then, declare the required permission by referring to [Requesting Application Permissions](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 48 492. Import the required modules. 50 513. Send an SMS message. 52 53```ts 54// Sample code 55import { sms } from '@kit.TelephonyKit'; 56import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 57 58let sendCallback: AsyncCallback<sms.ISendShortMessageCallback> = (err: BusinessError, data: sms.ISendShortMessageCallback) => { 59 console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 60} 61let deliveryCallback: AsyncCallback<sms.IDeliveryShortMessageCallback> = (err: BusinessError, data: sms.IDeliveryShortMessageCallback) => { 62 console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 63} 64let slotId: number = 0; 65let content: string = 'Message content'; 66let destinationHost: string = '+861xxxxxxxxxx'; 67let serviceCenter: string = '+861xxxxxxxxxx'; 68let destinationPort: number = 1000; 69let options: sms.SendMessageOptions = {slotId, content, destinationHost, serviceCenter, destinationPort, sendCallback, deliveryCallback}; 70sms.sendShortMessage(options, (err: BusinessError) => { 71 console.log(`callback: err->${JSON.stringify(err)}`); 72}); 73 74``` 75<!--DelEnd--> 76 77## How to Develop 78 79The API for sending SMS messages can be called only after the required system permission is granted. For a third-party application, it also needs to implement the function of redirecting to the SMS editing page and obtaining the edited content and recipient number by calling the **startAbility** API. 80 81```ts 82// Sample code 83import { common, Want } from '@kit.AbilityKit'; 84 85const MMS_BUNDLE_NAME = "com.ohos.mms"; 86const MMS_ABILITY_NAME = "com.ohos.mms.MainAbility"; 87const MMS_ENTITIES = "entity.system.home"; 88 89export class Contact { 90 contactsName: string; 91 telephone: number; 92 93 constructor(contactsName: string, telephone: number) { 94 this.contactsName = contactsName; 95 this.telephone = telephone; 96 } 97} 98 99@Entry 100@Component 101struct JumpMessage { 102 private context = getContext(this) as common.UIAbilityContext; 103 104 startMMSAbilityExplicit() { 105 // Complete the contact and number. You can query the contact name based on the phone number. Therefore, the phone number is mainly used in this mode. 106 let params: Array<Object> = [new Contact ("Tom", 13344556677)]; 107 108 let want: Want = { 109 bundleName: "com.ohos.mms", 110 abilityName: "com.ohos.mms.MainAbility", 111 parameters: { 112 contactObjects: JSON.stringify(params), 113 pageFlag: "conversation", 114 // Enter the SMS message content. 115 content: "SMS message content" 116 } 117 }; 118 119 this.context.startAbilityForResult(want).then((data) => { 120 console.log("Success" + JSON.stringify(data)); 121 }).catch(() => { 122 console.log("error"); 123 }); 124 } 125 126 build() { 127 Row() { 128 Column() { 129 Button ('Send SMS') 130 .onClick(() => { 131 this.startMMSAbilityExplicit(); 132 }) 133 } 134 .width('100%') 135 } 136 .height('100%') 137 } 138} 139 140``` 141