admin管理员组文章数量:1391955
I wanna make an app that its main functionality depends on sending SMS. Im using Flutter to make it for both IOS and Android.
In Android, it's possible to send sms in the background if you get user permission. but after searching through the net I couldn't find a way to do it in a flutter. I'm wondering if there is any way to send SMS in the background for Android using flutter.
after all, is there anyway( library, ...) that can sends sms in background. I am attaching the snapshot of the main.dart and MainActivity.kt files.
class SendSms extends StatefulWidget {
const SendSms({super.key});
@override
_SendSmsState createState() => _SendSmsState();
}
class _SendSmsState extends State<SendSms> {
static const platform = MethodChannel('sendSms');
List<String> phoneNumbers = [
"+923000000000",
];
Timer? _timer;
@override
void initState() {
super.initState();
_timer = Timer.periodic(const Duration(seconds: 2), (timer) {
sendSms();
});
}
Future<void> sendSms() async {
for (String number in phoneNumbers) {
try {
final String result = await platform.invokeMethod(
'send',
<String, dynamic>{
"phone": number,
"msg": "Automatic message",
},
);
if (kDebugMode) {
print("Message sent to $number: $result");
}
} on PlatformException catch (e) {
if (kDebugMode) {
print("Failed to send SMS to $number: ${e.toString()}");
}
}
}
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("SMS App")),
body: const Center(
child: Text("Sending SMS every 2 seconds..."),
),
);
}
}
MainActivity.kt
class MainActivity : FlutterActivity() {
private val CHANNEL = "sendSms"
private val SMS_PERMISSION_CODE = 1
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
print("Method call: ${call.method}");
if (call.method == "send") {
val phone: String? = call.argument("phone")
val msg: String? = call.argument("msg")
if (!phone.isNullOrEmpty() && !msg.isNullOrEmpty()) {
if (checkSmsPermission()) {
sendSMS(phone, msg, result)
} else {
requestSmsPermission(result)
}
} else {
result.error("INVALID_ARGUMENTS","Phone number or message is null", null)
}
} else {
result.notImplemented()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == SMS_PERMISSION_CODE) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.d("SMS_PERMISSION", "Permission granted")
} else {
Log.e("SMS_PERMISSION", "Permission denied")
}
}
}
private fun checkSmsPermission(): Boolean {
return ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED
}
private fun requestSmsPermission(result: MethodChannel.Result) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SEND_SMS), SMS_PERMISSION_CODE)
result.error("PERMISSION_DENIED", "SMS permission is required", null)
}
private fun sendSMS(phoneNo: String, msg: String, result: MethodChannel.Result) {
try {
val smsManager: SmsManager = SmsManager.getDefault()
smsManager.sendTextMessage(phoneNo, null, msg, null, null)
result.success("SMS Sent")
} catch (e: Exception) {
Log.e("SMS_ERROR", "Failed to send SMS", e)
result.error("ERR", "Sms Not Sent", e.localizedMessage)
}
}
}
I wanna make an app that its main functionality depends on sending SMS. Im using Flutter to make it for both IOS and Android.
In Android, it's possible to send sms in the background if you get user permission. but after searching through the net I couldn't find a way to do it in a flutter. I'm wondering if there is any way to send SMS in the background for Android using flutter.
after all, is there anyway( library, ...) that can sends sms in background. I am attaching the snapshot of the main.dart and MainActivity.kt files.
class SendSms extends StatefulWidget {
const SendSms({super.key});
@override
_SendSmsState createState() => _SendSmsState();
}
class _SendSmsState extends State<SendSms> {
static const platform = MethodChannel('sendSms');
List<String> phoneNumbers = [
"+923000000000",
];
Timer? _timer;
@override
void initState() {
super.initState();
_timer = Timer.periodic(const Duration(seconds: 2), (timer) {
sendSms();
});
}
Future<void> sendSms() async {
for (String number in phoneNumbers) {
try {
final String result = await platform.invokeMethod(
'send',
<String, dynamic>{
"phone": number,
"msg": "Automatic message",
},
);
if (kDebugMode) {
print("Message sent to $number: $result");
}
} on PlatformException catch (e) {
if (kDebugMode) {
print("Failed to send SMS to $number: ${e.toString()}");
}
}
}
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("SMS App")),
body: const Center(
child: Text("Sending SMS every 2 seconds..."),
),
);
}
}
MainActivity.kt
class MainActivity : FlutterActivity() {
private val CHANNEL = "sendSms"
private val SMS_PERMISSION_CODE = 1
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
print("Method call: ${call.method}");
if (call.method == "send") {
val phone: String? = call.argument("phone")
val msg: String? = call.argument("msg")
if (!phone.isNullOrEmpty() && !msg.isNullOrEmpty()) {
if (checkSmsPermission()) {
sendSMS(phone, msg, result)
} else {
requestSmsPermission(result)
}
} else {
result.error("INVALID_ARGUMENTS","Phone number or message is null", null)
}
} else {
result.notImplemented()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == SMS_PERMISSION_CODE) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.d("SMS_PERMISSION", "Permission granted")
} else {
Log.e("SMS_PERMISSION", "Permission denied")
}
}
}
private fun checkSmsPermission(): Boolean {
return ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED
}
private fun requestSmsPermission(result: MethodChannel.Result) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SEND_SMS), SMS_PERMISSION_CODE)
result.error("PERMISSION_DENIED", "SMS permission is required", null)
}
private fun sendSMS(phoneNo: String, msg: String, result: MethodChannel.Result) {
try {
val smsManager: SmsManager = SmsManager.getDefault()
smsManager.sendTextMessage(phoneNo, null, msg, null, null)
result.success("SMS Sent")
} catch (e: Exception) {
Log.e("SMS_ERROR", "Failed to send SMS", e)
result.error("ERR", "Sms Not Sent", e.localizedMessage)
}
}
}
Share
Improve this question
edited Mar 14 at 15:24
Frank van Puffelen
601k85 gold badges890 silver badges860 bronze badges
Recognized by Mobile Development Collective
asked Mar 14 at 5:50
Muhammad OsamaMuhammad Osama
435 bronze badges
1 Answer
Reset to default -1I also developed an application that listens to SMS. I did a lot of research but I couldn't solve this with Flutter. And I did it with Kotlin.
If the application is going to be open all the time, you need to get permission from the user to ignore battery optimization.
Don't fet to get permission for SMS too. (These may be sensitive permissions --> https://support.google/googleplay/android-developer/answer/9888170?hl=en) If you want to review it, you can look here ->> https://github/dcicek/read_sms
本文标签: How to send a periodic SMS on Android in background in FlutterStack Overflow
版权声明:本文标题:How to send a periodic SMS on Android in background in Flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744672258a2618894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论