Skip to content

[url_launcher] README instructions for SMS are incorrect #156281

Description

@onhyeon618

What package does this bug report belong to?

url_launcher

What target platforms are you seeing this bug on?

Android

Have you already upgraded your packages?

Yes

Dependency versions

pubspec.lock
name: test_flutter_features
description: "A new Flutter project."
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.3.4 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  url_launcher: 6.3.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.0

flutter:
  uses-material-design: true

Steps to reproduce

  1. Create an Uri with "sms" scheme, set message to any string that contains character "&".
  2. run launchUrl(uri, mode: LaunchMode.externalApplication)

Expected results

The message given to Uri properly shows on sms body field.

Actual results

The message is not shown as it's given on the Uri.
If the original message was "Hello & World", depending on how I created Uri, it appears either "Hello%20%26%20World" or "Hello ".

final uri = Uri(
  scheme: 'sms',
  queryParameters: <String, String>{
    'body': Uri.encodeComponent('Hello & World'),
  },
);  // result: "Hello%20%26%20World"
final message = Uri.encodeComponent('Hello & World');
final uri = Uri.parse('sms:?body=$message');  // result: "Hello "

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () async {
                /// Code example provided on README
                final uri = Uri(
                  scheme: 'sms',
                  queryParameters: <String, String>{
                    'body': Uri.encodeComponent('Hello & World'),
                  },
                );
                launchUrl(uri, mode: LaunchMode.externalApplication); // result: "Hello%20%26%20World"
              },
              child: const Text('Test 1'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                /// Another way that I tried
                final message = Uri.encodeComponent('Hello & World');
                final uri = Uri.parse('sms:?body=$message');
                launchUrl(uri, mode: LaunchMode.externalApplication); // result: "Hello "
              },
              child: const Text('Test 2'),
            ),
          ],
        ),
      ),
    );
  }
}

Screenshots or Videos

No response

Logs

No response

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.16.9, on Microsoft Windows [Version 10.0.19045.4894], locale ko-KR)
    • Flutter version 3.16.9 on channel stable at D:\Programs\Android\Flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 41456452f2 (8 months ago), 2024-01-25 10:06:23 -0800
    • Engine revision f40e976bed
    • Dart version 3.2.6
    • DevTools version 2.28.5

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\user\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
    • All Android licenses accepted.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2023.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)

[√] Connected device (4 available)
    • SM S901N (mobile)            • R5CT22YWE0P   • android-arm64  • Android 14 (API 34)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.19045.4894]
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 129.0.2792.65

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Activity

  1. changed the title [-]SMS body containing "&" doesn't work as expected on Android[/-] [+][url_launcher] SMS body containing "&" doesn't work as expected on Android[/+] on Oct 5, 2024
  2. stuartmorgan-g commented on Oct 5, 2024

    @stuartmorgan-g
    Contributor

    I somehow missed in reviewing flutter-team-archive/plugins#6042 that it was adding entirely new instructions for SMS. Those instructions are incorrect, and will double-encode the body; I'll update the README to fix that.

    The correct version would look like the mailto example in the README, using the query: encodeQueryParameters(...) construction.

  3. changed the title [-][url_launcher] SMS body containing "&" doesn't work as expected on Android[/-] [+][url_launcher] README instructions for SMS are incorrect[/+] on Oct 5, 2024
  4. added
    p: url_launcherPlugin to launch external applications
    packageflutter/packages repository. See also p: labels.
    P1High-priority issues at the top of the work list
    on Oct 5, 2024
  5. onhyeon618 commented on Oct 6, 2024

    @onhyeon618
    Author

    Hi @stuartmorgan, thank you for the quick response but I found the issue still exists with query: encodeQueryParameters(...) approach.

    String? encodeQueryParameters(Map<String, String> params) {
      return params.entries
          .map((MapEntry<String, String> e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
          .join('&');
    }
    
    final uri = Uri(
      scheme: 'sms',
      query: encodeQueryParameters(<String, String>{
        'body': 'Hello & World',
      }),
    );

    I tried this code, but the message body is shown as "Hello " instead of "Hello & World". By the point space behind "Hello" remains, it looks like everything behind "&" is truncated. Could you please be kind to let me know if I'm still misunderstanding the instruction?

    Plus, I found the message "Hello & World" works well on Android native code if I give it as extra of intent (putExtra("sms_body", message). It looks like that url_launcher sets everyting as intent's data(setData(Uri.parse(url))), could that be related somehow?

  6. stuartmorgan-g commented on Oct 6, 2024

    @stuartmorgan-g
    Contributor

    I tried this code, but the message body is shown as "Hello " instead of "Hello & World". By the point space behind "Hello" remains, it looks like everything behind "&" is truncated. Could you please be kind to let me know if I'm still misunderstanding the instruction?

    That new code is doing correct escaping. If the receiving phone app mis-parses the body of correctly encoded URLs, you'd need to file an issue with the developer of that app; url_launcher cannot control what happens after the URL is passed to the OS.

    It looks like that url_launcher sets everyting as intent's data

    url_launcher is not intended as a general intent library, it's for simple URL handoff.

  7. onhyeon618 commented on Oct 6, 2024

    @onhyeon618
    Author

    I tried it with basic sms app of Samsung Galaxy S22. Then it must be another Samsung device issue I guess...

    I will try to find some other solution for it. Thank you so much for the kind explanations.

  8. github-actions commented on Oct 22, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  9. locked as resolved and limited conversation to collaborators on Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1High-priority issues at the top of the work listp: url_launcherPlugin to launch external applicationspackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions