📱 Flutter SMS Background Messenger Send SMS messages from your Flutter apps with ease. Android-compatible plugin with built-in permission handling and background sending capabilities.
Is it work for flutter_background_service: ^5.1.0 flutter_local_notifications: ^18.0.1 flutter_background_messenger: ^0.0.2 Background Service in android??. import 'dart:async'; import 'dart:io'; import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:sms_background/bgservice/bg_service.dart'; import '../utils/color.dart'; // Custom colors or just use default // Initialize the background service Future<void> initializeService() async { final service = FlutterBackgroundService(); const AndroidNotificationChannel channel = AndroidNotificationChannel( 'my_foreground', // id 'MY FOREGROUND SERVICE', // title description: 'This channel is used for important notifications.', importance: Importance.low, enableVibration: false, enableLights: true, ledColor: primary, // Customize if needed ); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); if (Platform.isIOS || Platform.isAndroid) { await flutterLocalNotificationsPlugin.initialize( const InitializationSettings( iOS: DarwinInitializationSettings(), android: AndroidInitializationSettings('@mipmap/ic_launcher'), ), ); } await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(channel); await service.configure( androidConfiguration: AndroidConfiguration( onStart: onStart, autoStart: true, isForegroundMode: true, notificationChannelId: 'my_foreground', initialNotificationTitle: 'Service Running', initialNotificationContent: 'Sending SMS...', foregroundServiceNotificationId: 888, foregroundServiceTypes: [AndroidForegroundType.dataSync], ), iosConfiguration: IosConfiguration( autoStart: true, onForeground: onStart, onBackground: onIosBackground, ), ); } @pragma('vm:entry-point') Future<bool> onIosBackground(ServiceInstance service) async { WidgetsFlutterBinding.ensureInitialized(); DartPluginRegistrant.ensureInitialized(); return true; } @pragma('vm:entry-point') void onStart(ServiceInstance service) async { DartPluginRegistrant.ensureInitialized(); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); if (service is AndroidServiceInstance) { service.on('setAsForeground').listen((event) { service.setAsForegroundService(); }); service.on('setAsBackground').listen((event) { service.setAsBackgroundService(); }); } service.on('stopService').listen((event) { service.stopSelf(); }); // Listen for data from the main isolate service.on('data').listen((event) async { if (event != null && event['recipients'] != null) { String message = event['message']??''; List<String> recipients = List<String>.from(event['recipients']); debugPrint('Received recipients in background service: ${recipients.length}'); //if (recipients.isNotEmpty) { await sendBulkSMSWithProgress(recipients, message, flutterLocalNotificationsPlugin, service); //} } }); } Future<void> sendBulkSMSWithProgress( List<String> recipients, String message, FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, ServiceInstance service) async { int batchSize = 100; // Send 100 messages at a time int totalSent = 0; int totalFailed = 0; // Show a notification for SMS sending progress flutterLocalNotificationsPlugin.show( 888, 'Sending SMS', 'Sending in progress...', const NotificationDetails( android: AndroidNotificationDetails( 'SMS', 'Data Sync', icon: '@drawable/ic_sms_notify', ongoing: true, ), ), ); MessagingExample mMessagingExample = MessagingExample(); List<String> smsList = ['+91xxxxxxxxxx','+9xxxxxxxxxx','+91xxxxxxxxxx','+91xxxxxxxxxx','+91xxxxxxxxxx']; for(int i=0;i<smsList.length;i++){ bool isSms = await mMessagingExample.sendSMS(smsList[i],message); String title = isSms == true?'Success: ${smsList[i]}':'Failed ${smsList[i]}'; if(isSms){ totalSent += (i+1); }else{ totalFailed += (i+1); } // Update progress notification after each batch flutterLocalNotificationsPlugin.show( 888, title, 'Sent $totalSent messages, failed $totalFailed', const NotificationDetails( android: AndroidNotificationDetails( 'SMS', 'Data Sync', icon: '@drawable/ic_sms_notify', ongoing: true, ), ), ); // Delay between batches to avoid overwhelming the system await Future.delayed(Duration(seconds: 40)); } // Notify user when done flutterLocalNotificationsPlugin.show( 888, 'SMS Sent', 'Successfully sent $totalSent messages!', const NotificationDetails( android: AndroidNotificationDetails( 'SMS', 'Data Sync', icon: '@drawable/ic_sms_notify', ongoing: false, ), ), ); service.stopSelf(); // Stop the service after the task is complete } ------------------------------------------------ import 'package:flutter/cupertino.dart'; import 'package:flutter_background_messenger/flutter_background_messenger.dart'; class MessagingExample { final messenger = FlutterBackgroundMessenger(); Future<bool> sendSMS(String mobileNumber,String message) async { try { final success = await messenger.sendSMS( phoneNumber: mobileNumber, message: message, ); if (success) { debugPrint('SMS sent successfully'); return true; } else { debugPrint('Failed to send SMS'); return false; } } catch (e) { debugPrint('Error sending SMS: $e'); return false; } } } Check the above sample
This issue appears to be discussing a feature request or bug report related to the repository. Based on the content, it seems to be still under discussion. The issue was opened by teamrahul and has received 13 comments.