Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.sms"} Add the following configuration to the script element on the page where the API will be called:
import sms from '@system.sms'
Or
var sms = require("@system.sms") Item | Description |
|---|---|
Applicable devices | Mobile phones and tablets |
Applicable regions | Global |
API | Description |
|---|---|
Sends SMS messages. Each time the API is called, a pop-up is displayed for the user to confirm the sending. An SMS message contains up to 70 characters. |
Description
Sends SMS messages. Each time the API is called, a pop-up is displayed for the user to confirm the sending. An SMS message contains up to 70 characters.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
address | string | Yes | Destination number to which an SMS message will be sent. The value starts with a plus sign (+) and other characters can only be digits and spaces. |
content | string | Yes | SMS message content, which can contain up to 70 characters. |
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Result codes returned by fail
Result Code | Description |
|---|---|
200 | Sending failed. |
201 | The user has rejected the request of sending an SMS message. |
202 | The parameter is empty or invalid. |
Sample code
message.send({
address:'10086',
content:'This is the SMS message content.',
success: function () {
console.log('handling success')
},
fail: function (data, code) {
console.log('handling fail, code = ${code}')
}
}) <template>
<!-- Only one root node is allowed in template. -->
<div class="container">
<div class="case-title">
<text class="title">Recipient</text>
</div>
<div class="mlr-container">
<input onchange="phoneChange" class="input-big" placeholder="please input address" />
</div>
<div class="case-title">
<text class="title">Message</text>
</div>
<div class="mlr-container">
<textarea onchange="contentChange" class="textarea" placeholder="please input content"></textarea>
</div>
<div class="mlr-container mt-btn">
<input onclick="send" type="button" value="Send message" class="btn-blue" />
</div>
</div>
</template>
<style lang="sass">
.container {
flex-direction: column;
padding-bottom: 50px;
}
.case-title {
margin-left: 32px;
margin-right: 32px;
margin-bottom: 20px;
margin-top: 40px;
flex-direction: column;
}
.title {
color: rgba(0, 0, 0, 0.6);
font-size: 28px;
font-weight: 500;
}
.mlr-container {
margin-right: 32px;
margin-left: 32px;
}
.input-big {
width: 100%;
background-color: #fff;
height: 100px;
border-radius: 50px;
padding-left: 30px;
font-size: 32px;
}
.textarea {
width: 100%;
height: 300px;
background-color: #fff;
padding: 20px 30px;
font-size: 32px;
border-radius: 16px;
}
.btn-blue {
background-color: #0faeff;
width: 100%;
height: 80px;
border-radius: 40px;
color: #fff;
font-size: 32px;
font-weight: 500;
}
</style>
<script>
import prompt from '@system.prompt';
import message from '@system.sms';
module.exports = {
public: {
phone: "",
content: "",
},
onInit: function () {
this.$page.setTitleBar({ text: 'message'});
},
phoneChange({ value }) {
this.phone = value;
},
contentChange({ value }) {
this.content = value;
},
send() {
if (!this.phone || !this.content) {
prompt.showToast({
message: 'please input address or content',
duration: 2000,
gravity: 'bottom'
});
return;
}
message.send({
address: this.phone,
content: this.content,
success: function () {
prompt.showToast({
message: 'success',
duration: 2000,
gravity: 'bottom'
});
},
fail: function (data, code) {
prompt.showToast({
message: 'fail',
duration: 2000,
gravity: 'bottom'
});
}
})
}
}
</script> The following figure shows the final effect.
