[英]Angular/Ionic4 How to send an sms without opening native sms App
因此,我一直在关注 Ionic Native SMS 插件的 github 回购( https://github.com/cordova-sms/cordova-sms-plugin ),并且我已经按照回购建议进行了配置:
var options = {
replaceLineBreaks: false, // true to replace \n by a new line, false by default
android: {
intent: 'INTENT' // send SMS with the native android SMS messaging
//intent: '' // send SMS without opening any other app
}
};
但是,当我在真实设备上测试它时,它仍然不发送短信。
谁能帮助我,我需要添加权限吗? 这是我到目前为止的代码
sendSms() {
let options = {
replaceLineBreaks: false, // true to replace \n by a new line, false by default
android: {
intent: '' // send SMS with the native android SMS messaging
// intent: '' // send SMS without opening any other app
}
};
this.sms.send('656225667', 'SMS Works', options).then(val => {
alert('It works');
});
}
您可以在不打开本机短信应用程序的情况下发送短信。 您需要使用 Android 权限来获取短信权限
使用这两个 function
checkSMSPermission() {
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.SEND_SMS).then(
result => console.log('Has permission?', result.hasPermission),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.SEND_SMS)
);
}
requestSMSPermission() {
// tslint:disable-next-line: max-line-length
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.SEND_SMS, this.androidPermissions.PERMISSION.BROADCAST_SMS]);
}
您还需要在 Android 清单中包含这些功能。
<uses-permission android:name="android.permission.SEND_SMS" />
然后是短信 function 本身
sendSMS() {
this.checkSMSPermission();
this.contactComponent.getContact();
const numberOne = this.contactComponent.mContacts[0].number;
const numberTwo = this.contactComponent.mContacts[1].number;
const numbeThree = this.contactComponent.mContacts[2].number;
const numberFour = this.contactComponent.mContacts[3].number;
const numberFive = this.contactComponent.mContacts[4].number;
console.log(numberOne);
// tslint:disable-next-line: max-line-length
const message = this.messageComponent.dangerMessage + ' my location is: lat: ' + this.latitude.toString() + 'lng: ' + this.longitude.toString();
console.log('number=' + numberOne + ', message= ' + message);
// CONFIGURATION
const options = {
replaceLineBreaks: false, // true to replace \n by a new line, false by default
android: {
intent: '' // send SMS with the native android SMS messaging
// intent: '' // send SMS without opening any other app
}
};
this.sms.send(numberOne, message, options).then(() => {
this.presentAlert('Success', 'message has been sent');
})
.catch(error => {
this.presentAlert('Error', 'Failed: ' + error);
});
}
您不是在发送 SMS,而是在创建发送 SMS 的INTENT
。
Intent 允许您通过在 Intent object 中描述您想要执行的简单操作(例如“查看地图”或“拍照”)来启动另一个应用程序中的活动。 这种类型的意图称为隐式意图,因为它不指定要启动的应用程序组件,而是指定一个操作并提供一些数据来执行该操作。
这意味着代码仅生成“意图”,然后将其传递给您的手机默认应用程序,由其处理。 它如何处理intent
仅取决于应用程序。 甚至可以有多个应用程序可以处理intent
,然后用户会得到一个选择对话框。 当 SMS 真正发送时,您的应用程序无法控制。
这实际上是一件好事,因此,如果您安装任何应用程序,您可以确定它不会向订阅服务发送 SMS,并且您需要支付 100 欧元的电话费。
在不打开本机应用程序的情况下发送短信的唯一方法是使用付费的第三方服务为您发送短信。
那里有很多,例如 Twilio 是一个受欢迎的提供商。
我认为您需要使用服务器端技术或通过云 function发送它,以便您可以控制它的安全性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.