【问题标题】:Telephony package listener not working in background电话包监听器不在后台工作
【发布时间】:2023-02-07 11:56:08
【问题描述】:

telephony 包在后台收听传入的 SMS 消息时遇到问题。 在我的应用程序中,我想在应用程序处于后台但侦听器不起作用时收听检索短信。我使用 workmanager 包作为后台服务。我做了一些调试测试,后台服务没有问题。 这是我的代码:

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

【问题讨论】:

  • 您收听 SMS 的目的是什么,您是否尝试从消息中读取 OTP?
  • @JenisNavadiya 不,我想在收到短信后弹出通知。为此,我需要消息的正文。

标签: flutter dart telephony


【解决方案1】:

需要在 pubspec.yaml 中更改或添加 git 链接,

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

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多