gpt4 book ai didi

Angular/Ionic4 如何在不打开 native 短信应用程序的情况下发送短信

转载 作者:行者123 更新时间:2023-12-01 03:05:18 24 4
gpt4 key购买 nike

所以我一直在关注 Ionic Native SMS 插件的 github repo(https://github.com/cordova-sms/cordova-sms-plugin),我已经配置了 repo 建议:

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');
});
}

最佳答案

您无需打开 native 短信应用即可发送短信。您需要使用 Android 权限来获取短信权限

使用这两个函数

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 Manifest 中包含这些功能。

<uses-permission android:name="android.permission.SEND_SMS" />

然后是短信功能本身

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);
});
}

关于Angular/Ionic4 如何在不打开 native 短信应用程序的情况下发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com