1

I want to make custom SMS app and for that purpose it should be default SMS app. In android we use this code to make app as default SMS app.

 Intent intent =   new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, 
                            myPackageName);
                    startActivity(intent);
        

Can somebody help how to achieve this in Flutter. Thank you

1 Answers1

4

Just create a method channel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.example.chat/chat"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "setDefaultSms") {
                try {
                    var intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                    intent.putExtra(
                            Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
                            <the app package name>
                    );
                    startActivity(intent);
                    result.success("Success")
                } catch (ex: Exception) {
                    result.error("UNAVAILABLE", "Setting default sms.", null)
                }
            } else {
                result.notImplemented()
            }
        }
    }

}

after that in your dart code call the method channel

static const platform = const MethodChannel("com.example.chat/chat");
  try {
      final result = await platform.invokeMethod('setDefaultSms');
      print("Result: $result");
    } on PlatformException catch (e) {
      print("Error: $e");
    }
  }

Note the above solution won't make your app qualify to a default SMS app You may need to add something to your manifest file for more info check out this post Check this out

Patrick Waweru
  • 197
  • 1
  • 13