Flutter Telephony 在除模拟器之外的真实硬件中无法接收短信

问题描述 投票:0回答:3

我正在使用电话依赖项

receive_sms
。 它在模拟器中工作正常,我收到发送的每条消息,但它无法在真正的硬件手机上工作。

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:telephony/telephony.dart';

onBackgroundMessage(SmsMessage message) {
  debugPrint("onBackgroundMessage called");
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _message = "";
  final telephony = Telephony.instance;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  onMessage(SmsMessage message) async {
    setState(() {
      _message = message.body ?? "Error reading message body.";
    });
  }

  onSendStatus(SendStatus status) {
    setState(() {
      _message = status == SendStatus.SENT ? "sent" : "delivered";
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.

    final bool? result = await telephony.requestPhoneAndSmsPermissions;

    if (result != null && result) {
      telephony.listenIncomingSms(
          onNewMessage: onMessage, onBackgroundMessage: onBackgroundMessage);
    }

    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(child: Text("Latest received SMS: $_message")),
          TextButton(
              onPressed: () async {
                await telephony.openDialer("123413453");
              },
              child: Text('Open Dialer'))
        ],
      ),
    ));
  }
}

我确保在 Android Manifest 文件中添加了正确的权限。我请求了

RECEIVE_SMS
BROADCAST_SMS
权限,它似乎正在工作,因为我可以在模拟器中接收消息。

我做错了什么?

flutter telephonymanager
3个回答
5
投票

在开始收听消息之前尝试请求许可

bool permissionsGranted = await telephony.requestPhoneAndSmsPermissions;

此处列出了所需的许可以及如何请求。

https://telephony.shoutakmulay.dev/permissions


0
投票

确保您在清单中添加了这些权限。

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

    <application>
        ...
        ...

        <receiver android:name="com.shounakmulay.telephony.sms.IncomingSmsReceiver"
            android:permission="android.permission.BROADCAST_SMS" android:exported="true">
            <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>

    </application>
</manifest>

0
投票

我开始意识到电话套餐无法读取使用 RCS 协议发送的 SMS 消息。然而,他们可以成功读取传统的非 RCS 短信,尽管有时需要快速扫描。为了解决这个特定问题,您可以选择停用 Messenger 应用程序中的 RCS 功能。以下链接概述了在 Android 上禁用 RCS 聊天的过程:在 Android 中禁用 RCS 聊天

© www.soinside.com 2019 - 2024. All rights reserved.