sid heart
sid heart

Reputation: 1149

Flutter Telephony not recieving sms in real hardware except emulator

I am using telephony dependencies for receive_sms. It is working fine in the emulator and I receive every message sent, but it is not working on a real hardware phone.

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'))
        ],
      ),
    ));
  }
}

I made sure the correct permissions are added in the Android Manifest file. I asked for the RECEIVE_SMS and BROADCAST_SMS permissions and it appears to be working since I can receive messages in the emulator.

What am I doing wrong?

Upvotes: 4

Views: 1928

Answers (3)

I've come to realize that telephony package are incapable of reading SMS messages sent using the RCS protocol. However, they can successfully read traditional non-RCS SMS, albeit sometimes with a quick scan. To address this particular issue, you have the option to deactivate RCS functionality within the Messenger application. The process for disabling RCS Chat on Android is outlined in the following link: Disable RCS chat in Android

Upvotes: 2

MANISH DAYMA
MANISH DAYMA

Reputation: 1242

Try asking for permission before you start listening to the message.

bool permissionsGranted = await telephony.requestPhoneAndSmsPermissions;

here are listed permission required and how to ask.

https://telephony.shounakmulay.dev/permissions

Upvotes: 5

Harish
Harish

Reputation: 748

Make sure you added these permission in manifest.

<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>

Upvotes: 0

Related Questions