flutterdarttelephony

Telephony package listener not working in background


I have a problem with the telephony package for listening to incoming SMS messages in the background. in my app, I want to listen to retrieving SMS when the app is in the background but the listener does not work. I used the workmanager package for background service. I did some debug tests and the background service has no problem. here is my code:

backgrounMessageHandler(SmsMessage message) async {
  print(message.body);
}

@pragma('vm:entry-point')
callBackDispacher() {
  bg.Workmanager().executeTask((taskName, inputData) async {
    Telephony telephony = Telephony.backgroundInstance;
    await telephony.requestSmsPermissions;
    telephony.listenIncomingSms(
        onNewMessage: backgrounMessageHandler,
        onBackgroundMessage: backgrounMessageHandler);
    return Future.value(true);
  });
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await bg.Workmanager().initialize(callBackDispacher, isInDebugMode: true);
  runApp(const MyApp());
}

Solution

  • Need to change or add a git link in pubspec.yaml,

    telephony: 
        git:
          url: https://github.com/definev/Telephony.git
          ref: develop