js-apis-sms.md 19.0 KB
Newer Older
S
shawn_he 已提交
1 2
# SMS

S
shawn_he 已提交
3
>**NOTE**
S
shawn_he 已提交
4
>
Z
zengyawen 已提交
5
>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
S
shawn_he 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18

## Modules to Import

```
import sms from '@ohos.telephony.sms';
```

## sms.createMessage<a name=sms.createMessage-callback></a>

createMessage\(pdu: Array<number\>, specification: string, callback: AsyncCallback<ShortMessage\>\): void

Creates an SMS message instance based on the protocol data unit (PDU) and the specified SMS protocol. This function uses an asynchronous callback to return the result.

S
shawn_he 已提交
19 20
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
21
**Parameters**
S
shawn_he 已提交
22

S
shawn_he 已提交
23
| Name       | Type                                              | Mandatory| Description                                                        |
S
shawn_he 已提交
24
| ------------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
25 26 27
| pdu           | Array&lt;number&gt;                                | Yes  | PDU, which is obtained from the received SMS message.                          |
| specification | string                                             | Yes  | SMS protocol type. <br>- **3gpp**: GSM/UMTS/LTE SMS<br>- **3gpp2**: CDMA SMS|
| callback      | AsyncCallback&lt;[ShortMessage](#ShortMessage)&gt; | Yes  | Callback used to return the result.                                                  |
S
shawn_he 已提交
28

S
shawn_he 已提交
29
**Example**
S
shawn_he 已提交
30

S
shawn_he 已提交
31 32 33 34 35 36 37 38
```
const specification = '3gpp';
// Display PDUs using numbers in an array, for example, [0x08, 0x91, ...].
const pdu = [0x08, 0x91];
sms.createMessage(pdu, specification, (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
S
shawn_he 已提交
39 40 41 42 43 44 45 46


## sms.createMessage<a name=sms.createMessage-promise></a>

createMessage\(pdu: Array<number\>, specification: string\): Promise<ShortMessage\>

Creates an SMS message instance based on the PDU and the specified SMS protocol. This function uses a promise to return the result.

S
shawn_he 已提交
47 48
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
49
**Parameters**
S
shawn_he 已提交
50

S
shawn_he 已提交
51
| Name       | Type               | Mandatory| Description                                                        |
S
shawn_he 已提交
52
| ------------- | ------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
53 54
| pdu           | Array&lt;number&gt; | Yes  | PDU, which is obtained from the received SMS message.                          |
| specification | string              | Yes  | SMS protocol type. <br>- **3gpp**: GSM/UMTS/LTE SMS<br>- **3gpp2**: CDMA SMS|
S
shawn_he 已提交
55

S
shawn_he 已提交
56
**Return Value**
S
shawn_he 已提交
57

S
shawn_he 已提交
58
| Type                                        | Description                             |
S
shawn_he 已提交
59 60
| -------------------------------------------- | --------------------------------- |
| Promise&lt;[ShortMessage](#ShortMessage)&gt; | Promise used to return the result.|
S
shawn_he 已提交
61

S
shawn_he 已提交
62
**Example**
S
shawn_he 已提交
63

S
shawn_he 已提交
64 65 66 67 68 69 70 71 72 73 74
```
const specification = '3gpp';
// Display PDUs using numbers in an array, for example, [0x08, 0x91, ...].
const pdu = [0x08, 0x91];
let promise = sms.createMessage(pdu, specification);
promise.then(data => {
    console.log(`createMessage success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`createMessage fail, promise: err->${JSON.stringify(err)}`);
});
```
S
shawn_he 已提交
75 76 77 78 79 80 81

## sms.sendMessage

sendMessage(options: SendMessageOptions): void

Sends an SMS message.

S
shawn_he 已提交
82 83 84
**Required permission**: ohos.permission.SEND_MESSAGES

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
85

S
shawn_he 已提交
86
**Parameters**
S
shawn_he 已提交
87

S
shawn_he 已提交
88
| Name | Type                                     | Mandatory| Description                                                        |
S
shawn_he 已提交
89
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
90
| options | [SendMessageOptions](#SendMessageOptions) | Yes  | Options (including the callback) for sending an SMS message. For details, see [SendMessageOptions](#SendMessageOptions).|
S
shawn_he 已提交
91

S
shawn_he 已提交
92
**Example**
S
shawn_he 已提交
93

S
shawn_he 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
```
let sendCallback = function (err, data) {    
    console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 
}
let deliveryCallback = function (err, data) {    
    console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`); 
}
let slotId = 0;
let content ='SMS message content';
let destinationHost = '+861xxxxxxxxxx';
let serviceCenter = '+861xxxxxxxxxx';
let destinationPort = 1000;
let options = {slotId, content, destinationHost, serviceCenter, destinationPort, sendCallback, deliveryCallback};
sms.sendMessage(options);
```
S
shawn_he 已提交
109 110 111 112 113 114 115 116


## sms.getDefaultSmsSlotId<sup>7+</sup><a name=sms.getDefaultSmsSlotId-callback></a>

getDefaultSmsSlotId\(callback: AsyncCallback<number\>\): void

Obtains the default slot of the SIM card used to send SMS messages. This function uses an asynchronous callback to return the result.

S
shawn_he 已提交
117 118
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
119
**Parameters**
S
shawn_he 已提交
120

S
shawn_he 已提交
121
| Name  | Type                       | Mandatory| Description                                    |
S
shawn_he 已提交
122
| -------- | --------------------------- | ---- | ---------------------------------------- |
S
shawn_he 已提交
123
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the result.<br>- **0**: card slot 1<br>- **1**: card slot 2|
S
shawn_he 已提交
124

S
shawn_he 已提交
125
**Example**
S
shawn_he 已提交
126

S
shawn_he 已提交
127 128 129 130 131
```
sms.getDefaultSmsSlotId((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
S
shawn_he 已提交
132 133 134 135 136 137 138 139


## sms.getDefaultSmsSlotId<sup>7+</sup><a name=sms.getDefaultSmsSlotId-promise></a>

getDefaultSmsSlotId\(\): Promise<number\>

Obtains the default slot of the SIM card used to send SMS messages. This function uses a promise to return the result.

S
shawn_he 已提交
140 141
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
142
**Return Value**
S
shawn_he 已提交
143

S
shawn_he 已提交
144
| Type           | Description                                                        |
S
shawn_he 已提交
145
| --------------- | ------------------------------------------------------------ |
S
shawn_he 已提交
146
| Promise<number> | Promise used to return the result.<br>- **0**: card slot 1<br>- **1**: card slot 2|
S
shawn_he 已提交
147

S
shawn_he 已提交
148
**Example**
S
shawn_he 已提交
149

S
shawn_he 已提交
150
```
S
shawn_he 已提交
151
let promise = sms.getDefaultSmsSlotId();
S
shawn_he 已提交
152 153 154 155 156 157
promise.then(data => {
    console.log(`getDefaultSmsSlotId success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`getDefaultSmsSlotId fail, promise: err->${JSON.stringify(err)}`);
});
```
S
shawn_he 已提交
158 159 160 161 162 163 164 165


## sms.setSmscAddr<sup>7+</sup><a name=sms.setSmscAddr-callback></a>

setSmscAddr\(slotId: number, smscAddr: string, callback: AsyncCallback<void\>\): void

Sets the short message service center (SMSC) address. This function uses an asynchronous callback to return the result.

S
shawn_he 已提交
166 167 168
**Required permission**: ohos.permission.SET_TELEPHONY_STATE (a system permission)

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
169

S
shawn_he 已提交
170
**Parameters**
S
shawn_he 已提交
171

S
shawn_he 已提交
172
| Name  | Type                     | Mandatory| Description                                     |
S
shawn_he 已提交
173
| -------- | ------------------------- | ---- | ----------------------------------------- |
S
shawn_he 已提交
174 175 176
| slotId   | number                    | Yes  | SIM card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
| smscAddr | string                    | Yes  | SMSC address.                        |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.                               |
S
shawn_he 已提交
177

S
shawn_he 已提交
178
**Example**
S
shawn_he 已提交
179

S
shawn_he 已提交
180 181 182 183 184 185 186
```
let slotId = 0;
let smscAddr = '+861xxxxxxxxxx';
sms.setSmscAddr(slotId, smscAddr, (err,data) => {
      console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
S
shawn_he 已提交
187 188 189 190 191 192 193 194


## sms.setSmscAddr<sup>7+</sup><a name=sms.setSmscAddr-promise></a>

setSmscAddr\(slotId: number, smscAddr: string\): Promise<void\>

Sets the SMSC address. This function uses a promise to return the result.

S
shawn_he 已提交
195 196 197
**Required permission**: ohos.permission.SET_TELEPHONY_STATE (a system permission)

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
198

S
shawn_he 已提交
199
**Parameters**
S
shawn_he 已提交
200

S
shawn_he 已提交
201
| Name  | Type  | Mandatory| Description                                     |
S
shawn_he 已提交
202
| -------- | ------ | ---- | ----------------------------------------- |
S
shawn_he 已提交
203 204
| slotId   | number | Yes  | SIM card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
| smscAddr | string | Yes  | SMSC address.                        |
S
shawn_he 已提交
205

S
shawn_he 已提交
206
**Return Value**
S
shawn_he 已提交
207

S
shawn_he 已提交
208
| Type               | Description                           |
S
shawn_he 已提交
209 210
| ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise used to return the result.|
S
shawn_he 已提交
211

S
shawn_he 已提交
212
**Example**
S
shawn_he 已提交
213

S
shawn_he 已提交
214 215 216 217 218 219 220 221 222 223
```
let slotId = 0;
let smscAddr = '+861xxxxxxxxxx';
let promise = sms.setSmscAddr(slotId, smscAddr);
promise.then(data => {
    console.log(`setSmscAddr success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`setSmscAddr fail, promise: err->${JSON.stringify(err)}`);
});
```
S
shawn_he 已提交
224 225 226 227 228 229 230 231


## sms.getSmscAddr<sup>7+</sup><a name=sms.getSmscAddr-callback></a>

getSmscAddr\(slotId: number, callback: AsyncCallback<string\>\): void

Obtains the SMSC address. This function uses an asynchronous callback to return the result.

S
shawn_he 已提交
232 233 234
**Required permission**: ohos.permission.GET_TELEPHONY_STATE (a system permission)

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
235

S
shawn_he 已提交
236 237
**Note:** This is a system API and it is used only for system applications.

S
shawn_he 已提交
238
**Parameters**
S
shawn_he 已提交
239

S
shawn_he 已提交
240
| Name  | Type                       | Mandatory| Description                                     |
S
shawn_he 已提交
241
| -------- | --------------------------- | ---- | ----------------------------------------- |
S
shawn_he 已提交
242 243
| slotId   | number                      | Yes  | SIM card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the result.                               |
S
shawn_he 已提交
244

S
shawn_he 已提交
245
**Example**
S
shawn_he 已提交
246

S
shawn_he 已提交
247 248 249 250 251 252
```
let slotId = 0;
sms.getSmscAddr(slotId, (err, data) => {
      console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
S
shawn_he 已提交
253 254 255 256 257 258 259 260


## sms.getSmscAddr<sup>7+</sup><a name=sms.getSmscAddr-promise></a>

getSmscAddr\(slotId: number\): Promise<string\>

Obtains the SMSC address. This function uses a promise to return the result.

S
shawn_he 已提交
261 262 263
**Required permission**: ohos.permission.GET_TELEPHONY_STATE (a system permission)

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
264

S
shawn_he 已提交
265 266
**Note:** This is a system API and it is used only for system applications.

S
shawn_he 已提交
267
**Parameters**
S
shawn_he 已提交
268

S
shawn_he 已提交
269
| Name| Type  | Mandatory| Description                                     |
S
shawn_he 已提交
270
| ------ | ------ | ---- | ----------------------------------------- |
S
shawn_he 已提交
271
| slotId | number | Yes  | SIM card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
S
shawn_he 已提交
272

S
shawn_he 已提交
273
**Return Value**
S
shawn_he 已提交
274

S
shawn_he 已提交
275
| Type                 | Description                                         |
S
shawn_he 已提交
276 277
| --------------------- | --------------------------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
S
shawn_he 已提交
278

S
shawn_he 已提交
279
**Example**
S
shawn_he 已提交
280

S
shawn_he 已提交
281 282 283 284 285 286 287 288 289
```
let slotId = 0;
let promise = sms.getSmscAddr(slotId);
promise.then(data => {
    console.log(`getSmscAddr success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`getSmscAddr fail, promise: err->${JSON.stringify(err)}`);
});
```
S
shawn_he 已提交
290

S
shawn_he 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
## sms.hasSmsCapability<sup>7+</sup><a name=sms.hasSmsCapability></a>

hasSmsCapability(): boolean

Checks whether the current device can send and receive SMS messages. This function works in synchronous mode.

**System capability**: SystemCapability.Telephony.SmsMms

**Return Value**

| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
| boolean | - **true**: The device can send and receive SMS messages.<br>- **false**: The device cannot send or receive SMS messages.|

```
let result = sms.hasSmsCapability(); 
console.log(`hasSmsCapability: ${JSON.stringify(result)}`);
```
S
shawn_he 已提交
309 310 311 312 313

## ShortMessage<a name=ShortMessage></a>

Defines an SMS message instance.

S
shawn_he 已提交
314 315
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
316
| Variable                    | Type                                   | Description                                                        |
S
shawn_he 已提交
317
| ------------------------ | --------------------------------------- | ------------------------------------------------------------ |
S
shawn_he 已提交
318 319 320 321 322 323 324 325
| hasReplyPath             | boolean                                 | Whether the received SMS contains **TP-Reply-Path**. The default value is **false**.<br>**TP-Reply-Path**: the path in which the mobile phone can reply to the SMS message through the originating SMSC.|
| isReplaceMessage         | boolean                                 | Whether the received SMS message is a **replace short message**. The default value is **false**.<br>For details, see section 9.2.3.9 in **3GPP TS 23.040**.|
| isSmsStatusReportMessage | boolean                                 | Whether the received SMS message is an SMS delivery status report. The default value is **false**.<br>**SMS-Status-Report**: a message sent from the SMSC to the mobile station to show the SMS message delivery status.|
| messageClass             | [ShortMessageClass](#ShortMessageClass) | SMS message type.                                                  |
| pdu                      | Array&lt;number&gt;                     | PDU in the SMS message.                           |
| protocolId               | number                                  | ID of the protocol used for sending SMS messages.                                  |
| scAddress                | string                                  | Address of the short message service center (SMSC).                                |
| scTimestamp              | number                                  | SMSC timestamp.                                                |
S
shawn_he 已提交
326
| status                   | number                                  | SMS message status sent by the SMSC in the **SMS-STATUS-REPORT** message.|
S
shawn_he 已提交
327 328
| visibleMessageBody       | string                                  | SMS message body.                                                  |
| visibleRawAddress        | string                                  | Sender address.                                                |
S
shawn_he 已提交
329 330 331 332 333 334


## ShortMessageClass<a name=ShortMessageClass></a>

Enumerates SMS message types.

S
shawn_he 已提交
335
| Variable            | Value  | Description                                                        |
S
shawn_he 已提交
336
| ---------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
337 338 339 340 341
| UNKNOWN          | 0    | Unknown type.<br>**System capability**: SystemCapability.Telephony.SmsMms|
| INSTANT_MESSAGE  | 1    | Instant message, which is displayed immediately after being received.<br>**System capability**: SystemCapability.Telephony.SmsMms|
| OPTIONAL_MESSAGE | 2    | Message stored in the device or SIM card.<br>**System capability**: SystemCapability.Telephony.SmsMms|
| SIM_MESSAGE      | 3    | Message containing SIM card information, which is to be stored in the SIM card.<br>**System capability**: SystemCapability.Telephony.SmsMms|
| FORWARD_MESSAGE  | 4    | Message to be forwarded to another device.<br>**System capability**: SystemCapability.Telephony.SmsMms|
S
shawn_he 已提交
342 343 344 345 346 347 348 349


## SendMessageOptions<a name=SendMessageOptions></a>

Provides the options (including callbacks) for sending an SMS message.

For example, you can specify the SMS message type by the optional parameter **content**.

S
shawn_he 已提交
350 351
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
352
| Parameter          | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
353
| ---------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
354 355 356 357 358 359 360
| slotId           | number                                                       | Yes  | Slot ID of the SIM card used for sending SMS messages. <br>- **0**: card slot 1<br>- **1**: card slot 2     |
| destinationHost  | string                                                       | Yes  | Destination address of the SMS message.                                            |
| content          | string \| Array&lt;number&gt;                                | Yes  | SMS message type. If the content is composed of character strings, the SMS message is a text message. If the content is composed of byte arrays, the SMS message is a data message.|
| serviceCenter    | string                                                       | No  | SMSC address. By default, the SMSC address in the SIM card is used.               |
| destinationPort  | number                                                       | No  | Destination port of the SMS message. This parameter is mandatory only for a data message. Otherwise, it is optional.  |
| sendCallback     | AsyncCallback&lt;[ISendShortMessageCallback](#ISendShortMessageCallback)&gt; | No  | Callback used to return the SMS message sending result. For details, see [ISendShortMessageCallback](#ISendShortMessageCallback).|
| deliveryCallback | AsyncCallback&lt;[IDeliveryShortMessageCallback](#IDeliveryShortMessageCallback)&gt; | No  | Callback used to return the SMS message delivery report. For details, see [IDeliveryShortMessageCallback](#IDeliveryShortMessageCallback).|
S
shawn_he 已提交
361 362 363 364


## ISendShortMessageCallback<a name=ISendShortMessageCallback></a>

S
shawn_he 已提交
365 366 367
Provides the callback for the SMS message sending result. It consists of three parts: SMS message sending result, URI for storing the sent SMS message, and whether the SMS message is the last part of a long SMS message.

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
368

S
shawn_he 已提交
369
| Parameter    | Type                           | Mandatory| Description                                                        |
S
shawn_he 已提交
370
| ---------- | ------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
371 372 373
| isLastPart | boolean                         | No  | Whether this SMS message is the last part of a long SMS message. The value **true** indicates that this SMS message is the last part of a long SMS message, and value **false** indicates the opposite. The default value is **false**.|
| result     | [SendSmsResult](#SendSmsResult) | Yes  | SMS message sending result.                                              |
| url        | string                          | Yes  | URI for storing sent SMS messages.                                         |
S
shawn_he 已提交
374 375 376 377


## IDeliveryShortMessageCallback<a name=IDeliveryShortMessageCallback></a>

S
shawn_he 已提交
378 379 380
Provides the callback for the SMS message sending result. Return the SMS delivery report.

**System capability**: SystemCapability.Telephony.SmsMms
S
shawn_he 已提交
381

S
shawn_he 已提交
382
| Parameter| Type               | Mandatory| Description          |
S
shawn_he 已提交
383
| ------ | ------------------- | ---- | -------------- |
S
shawn_he 已提交
384
| pdu    | Array&lt;number&gt; | Yes  | SMS message delivery report.|
S
shawn_he 已提交
385 386 387 388 389 390


## SendSmsResult<a name=SendSmsResult></a>

Enumerates SMS message sending results.

S
shawn_he 已提交
391 392
**System capability**: SystemCapability.Telephony.SmsMms

S
shawn_he 已提交
393
| Parameter                              | Value  | Description                                                  |
S
shawn_he 已提交
394
| ------------------------------------ | ---- | ------------------------------------------------------ |
S
shawn_he 已提交
395 396 397
| SEND_SMS_SUCCESS                     | 0    | SMS message sent successfully.                                        |
| SEND_SMS_FAILURE_UNKNOWN             | 1    | Failed to send the SMS message due to unknown reasons.                              |
| SEND_SMS_FAILURE_RADIO_OFF           | 2    | Failed to send the SMS message because the modem is shut down.                  |
S
shawn_he 已提交
398
| SEND_SMS_FAILURE_SERVICE_UNAVAILABLE | 3    | Failed to send the SMS message because the network is unavailable or SMS message sending or receiving is not supported.|