The 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.
Typical development scenarios are as follows:
On web pages:
On applications:
SMS
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.
SMSC
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.
PDU
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.
The SMS service is available only on standard-system devices.
An available SIM card must be present on the device, and the permission to send SMS messages must be granted.
To ensure app running efficiency, most API calls are asynchronous. For asynchronous APIs, both callback and promise modes are provided. The following examples use the callback mode. For other calling modes, see @ohos.telephony.sms (SMS service).
| Name | Description |
|---|---|
| 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. |
| createMessage(pdu: Array<number>, specification: string, callback: AsyncCallback<ShortMessage>): void | Creates an SMS message instance based on the PDU and the specified SMS protocol. |
| getDefaultSmsSlotId(callback: AsyncCallback<number>): void | Obtains the ID of the default SIM card used to send SMS messages. |
The API for sending SMS messages requires system permissions to call. If a third-party app needs to send SMS messages, it must implement the function of redirecting to the SMS message editing page within the app, and carry the message content and recipient number. This can be implemented by calling the startAbility API of the Ability framework to specify the number and redirect to the SMS message sending page. After obfuscation is enabled, contactsName and telephone may be obfuscated. It is recommended that you retain the property names in -keep-property-name.
// Sample code
import { common, Want } from '@kit.AbilityKit';
const MMS_BUNDLE_NAME = "com.ohos.mms";
const MMS_ABILITY_NAME = "com.ohos.mms.MainAbility";
const MMS_ENTITIES = "entity.system.home";
export class Contact {
contactsName: string;
telephone: number;
constructor(contactsName: string, telephone: number) {
this.contactsName = contactsName;
this.telephone = telephone;
}
}
@Entry
@Component
struct JumpMessage {
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
startMMSAbilityExplicit() {
// 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.
let params: Array<Object> = [new Contact("Tom", 133XXXXXXXX)];
let want: Want = {
bundleName: "com.ohos.mms",
abilityName: "com.ohos.mms.MainAbility",
parameters: {
contactObjects: JSON.stringify(params), // After obfuscation is enabled, contactsName and telephone may be obfuscated. It is recommended that you preserve the property names in -keep-property-name.
pageFlag: "conversation",
// Enter the SMS message content.
content: "SMS message content"
}
};
this.context.startAbilityForResult(want).then((data) => {
console.info("Success" + JSON.stringify(data));
}).catch(() => {
console.error("error");
});
}
build() {
Row() {
Column() {
Button ('Send SMS')
.onClick(() => {
this.startMMSAbilityExplicit();
})
}
.width('100%')
}
.height('100%')
}
} Through the SMS protocol, you can create a hyperlink pointing to the SMS recipient so that users can directly navigate to the SMS application through the hyperlink on a web page or an application. In addition, you can define the SMS message recipient and content in the sms: field to save the SMS message editing time.
The standard SMS protocol format is as follows:
sms:106XXXXXXXXXX?body=SMS message content
sms:: SMS scheme, which is mandatory.
106XXXXXXXXXX: recipient number, which is optional. If there are multiple addresses, separate them with commas (,).
?: start declaration character of the SMS message content. This parameter is mandatory if the SMS message content is present.
body-value: SMS message content, which is optional.
Starting from a Web Page
Hyperlinks on web pages must comply with the SMS protocol. The sample code is as follows:
<a href="sms:106XXXXXXXXXX?body=%E5%8F%91%E9%80%81%E7%9F%AD%E4%BF%A1%E5%86%85%E5%AE%B9">Send Message</a>;
In actual development, replace the recipient number with the actual number. The SMS message content can be configured as required.
Starting from an Application
Pass the sms string to the uri parameter. In the application, the context can be obtained through this.getUIContext().getHostContext() for a page and through this.context for an ability.
// Sample code
import { common, Want } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
build() {
Column() {
Button ('Send SMS')
.onClick(() => {
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let exampleUrl = "sms:106XXXXXXXXXX?body=%E5%8F%91%E9%80%81%E7%9F%AD%E4%BF%A1%E5%86%85%E5%AE%B9";
let want: Want = {
bundleName: 'com.ohos.mms',
action: 'ohos.want.action.viewData',
uri:exampleUrl,
}
context.startAbility(want).then((data) => {
console.info("Success" + JSON.stringify(data));
}).catch(() => {
console.error("error");
});
})
}
}
}