Skip to content

[BUG] Custom URI Stops Working after MSIX Packaging #187

Description

@cliftonlabrum

ℹ️ Info

Version: 3.9.1

💬 Description

I can't get my Flutter app's custom URI to work after I package my app with MSIX, and I'm trying to figure out if it's a bug with protocol_handler that creates and registers the URI, or if it's something to do with MSIX.

Here's what I've done:

  1. I run my Flutter app which registers a custom URI.
  2. I run the app in the debugger.
  3. Hitting my custom URI in a web browser, such as myapp://test opens my app.
  4. After packaging with flutter pub run msix:create and signing the MSIX, then running the app on another computer, the app runs, but hitting the custom URI does not open the app.

I have confirmed the issue across two apps:

  1. The first app uses a redirect from the Dropbox API – works in debug mode, fails after MSIX packaging.
  2. The second app uses an auth redirect from Supabase – works in debug mode, fails after MSIX packaging.

📜 Pubspec.yaml

msix_config:
  display_name: Pilot Pro
  publisher_display_name: Labrum Co
  identity_name: LabrumCo.PilotPro
  publisher: CN=<*****>
  msix_version: 5.7.7.0
  logo_path: C:\Users\clift\OneDrive\Desktop\PilotPro\assets\images\windows.png
  store: true

🖥️ Sample Project

Below is a minimal sample project. You will need to add these two packages:

flutter pub add msix
flutter pub add protocol_handler

After building the app on Windows, visit uritest://test in a web browser, and it should show this:

uritest

After clicking Open uri_test it will bring the app to the forefront and you should see this in the debug console:

Got it: uritest://test/

Here are the contents of main.dart:

import 'package:flutter/material.dart';
import 'package:protocol_handler/protocol_handler.dart';

void main() async {
  // Must add this line.
  WidgetsFlutterBinding.ensureInitialized();

  // Register a custom protocol
  await protocolHandler.register('uritest');

  runApp(const MyApp());
}

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

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

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

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

class _MyHomePageState extends State<MyHomePage> with ProtocolListener {
  @override
  void initState() {
    protocolHandler.addListener(this);
    super.initState();
  }

  @override
  void dispose() {
    protocolHandler.removeListener(this);
    super.dispose();
  }

  @override
  void onProtocolUrlReceived(String url) {
    //::::: Confirm URL is received :::::
    String log = 'Got it: $url';
    print(log);
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text('Custom URI Test'),
      ),
    );
  }
}

You have to add the following to /windows/runner/main.cpp:

//Add this include at the top...
#include <protocol_handler/protocol_handler_plugin.h>

//Just inside the existing method here...
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t *command_line, _In_ int show_command) {
  
  //Add this...
  HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"uri_test");
  if (hwnd != NULL) {
   DispatchToProtocolHandler(hwnd);

    ::ShowWindow(hwnd, SW_NORMAL);
    ::SetForegroundWindow(hwnd);
    return EXIT_FAILURE;
  }
...

After you package this with MSIX, you must launch it on another user account or computer. This is because (as far as I can tell) the registry edits are already made on your machine with the debug version of the app running.

Could you please check if this is something to do with MSIX or not? This is blocking me on launching two apps on Windows. 😅 Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions