flutter_local_notifications icon indicating copy to clipboard operation
flutter_local_notifications copied to clipboard

[flutter_local_notifications] [WIP] Windows implementation of notifications

Open kennethnym opened this issue 3 years ago • 38 comments

This draft PR contains Windows implementation of this plugin. Still a WIP.

  • [ ] initialize WIP
  • [ ] show WIP
  • [ ] periodicallyShow (not sure if possible due to limitation of win32)
  • [x] cancel
  • [x] cancelAll
  • [ ] pendingNotificationRequests
  • [x] onSelectNotification WIP

cc @azchohfi Related issue: #746

kennethnym avatar Feb 09 '22 22:02 kennethnym

Hey @kennethnym, I've cloned your branch and built the source, and a simple plain notification worked totally fine for me. The problem with your particular code is the template=ToastGeneric. If you switch that to, for example, ToastText01, then it works fine. This value needs to be one of the valid TileTemplateType

azchohfi avatar Feb 10 '22 01:02 azchohfi

I tried using the default templates but it would just show "New notification" instead of the actual body.

kennethnym avatar Feb 10 '22 08:02 kennethnym

@azchohfi To illustrate, this is the code that I have at the moment:

const auto doc = winrt::Windows::UI::Notifications::ToastNotificationManager::GetTemplateContent(winrt::Windows::UI::Notifications::ToastTemplateType::ToastText01);
const auto nodes = doc.GetElementsByTagName(L"text");
nodes.Item(0).AppendChild(doc.CreateTextNode(winrt::to_hstring(title)));

winrt::Windows::UI::Notifications::ToastNotification notif{ doc };
const auto notifier = winrt::Windows::UI::Notifications::ToastNotificationManager::CreateToastNotifier(L"com.dexterous.example");

notifier.Show(notif);

and this is the toast shown: image

title is 'plain title', but it is not shown in the notification.

kennethnym avatar Feb 10 '22 18:02 kennethnym

Oh, I see. The parameters are being sent from dart to C++, but your GetString method is not querying them properly. You should wrap the key inside an EncodableValue:

	const auto pair = m->find(flutter::EncodableValue(key));

Also, when you call the GetString method, you should store it not using a reference(&):

		const auto body = Utils::GetString("body", args).value();

You can use Visual Studio 2022 or 2019 to attach to the running exe and debug the C++ code. There is also a solution at "flutter_local_notifications\flutter_local_notifications\example\build\windows\example.sln" that will have the reference to all the files so you can add breakpoints, etc.

I've also found another issue. You need to add the id's to the texts, like this:

		XmlDocument doc;
		doc.LoadXml(L"\
			<toast>\
				<visual>\
					<binding template=\"ToastText01\">\
						<text id=\"1\">abc</text>\
						<text id=\"2\">def</text>\
					</binding>\
				</visual>\
			</toast>");

You can also debug this using cout:

std::cout << winrt::to_string(doc.GetXml()) << std::endl;

I've tested this changes here and now the notifications shows properly: image

Let me know if this helps!

azchohfi avatar Feb 10 '22 20:02 azchohfi

Ahh thanks, that makes sense now. I am not a C++ programmer, so I really appreciate your help!

kennethnym avatar Feb 10 '22 21:02 kennethnym

Hi, is it working as expected now after your latest commits?

lzzy12 avatar Feb 14 '22 10:02 lzzy12

Basic title body notification is working, but nothing beyond that.

kennethnym avatar Feb 14 '22 10:02 kennethnym

@lzzy12 Just an update: I have implemented cancel and cancelAll in the latest commit.

kennethnym avatar Feb 14 '22 11:02 kennethnym

FYI this reminds me that zonedSchedule currently part of the platform interface but should be so that will happen soon. Note that there's a 10.0.0 branch right now for the 10.0.0 prerelease

Edit: had trouble adding zonedSchedule to platform interface as overriding the method and having a required parameter isn't possible

MaikuB avatar Feb 21 '22 09:02 MaikuB

@kennethnym any updates on this PR?

azchohfi avatar Mar 01 '22 19:03 azchohfi

Hi sorry, busy with coursework at the moment, I will continue the work this weekend.

kennethnym avatar Mar 01 '22 19:03 kennethnym

Just a bit of progress update: I am currently in the process of implementing onSelectNotification callback.

Update: onSelectNotification now works, but only when the program is active.

kennethnym avatar Mar 05 '22 14:03 kennethnym

At the moment, onSelectNotification is only called once. After a toast is clicked and the callback is invoked, for some reason Windows would not invoke the callback anymore whenever subsequent toasts are clicked on. I am investigating what is going wrong.

kennethnym avatar Mar 06 '22 18:03 kennethnym

Sorry for the silence, I am currently busy with courseworks. At the moment I am still stuck with the issue above. I would really appreciate it If anyone can help me with it.

kennethnym avatar Mar 23 '22 22:03 kennethnym

Hello @kennethnym. I've tried your branch and I managed to make it work more than once. Cherry-pick this commit from my fork: https://github.com/azchohfi/flutter_local_notifications/commit/d363e5c7e314a2a484996d42100d89cfaeb7b59e

azchohfi avatar Mar 28 '22 21:03 azchohfi

Ahh i see the issue now, its REGCLS_MULTIPLEUSE vs REGCLS_SINGLEUSE. I can't believe I have missed such an obvious thing. Thank you so much.

kennethnym avatar Mar 28 '22 21:03 kennethnym

@kennethnym I tried your WIP branch and had some issues with notifications and msix packaging, @azchohfi found the culprit, https://github.com/YehudaKremer/msix/issues/118#issuecomment-1081134367 something to keep in mind. Looks like we might have to use the correct CreateToastNotifier method based on if the app is packaged or not.

sidevesh avatar Apr 03 '22 15:04 sidevesh

Thanks for bring that to attention, I'll investigate how to handle msix apps as well.

kennethnym avatar Apr 03 '22 15:04 kennethnym

@sidevesh Can you test if the latest commit fixes your issue?

kennethnym avatar Apr 13 '22 21:04 kennethnym

I get the following error when building an app with the latest commit:

C:\Users\test\Projects\Side-projects\tomatoro\tomatoro-app\windows\flutter\ephemeral\.plugin_symlinks\flutter_local_notifications\windows\flutter_local_notifications_plugin.cpp(224): error C2220: the 
following warning is treated as an error [C:\Users\test\Projects\Side-projects\tomatoro\tomatoro-app\build\windows\plugins\flutter_local_notifications\flutter_local_notifications_plugin.vcxproj]      
C:\Users\test\Projects\Side-projects\tomatoro\tomatoro-app\windows\flutter\ephemeral\.plugin_symlinks\flutter_local_notifications\windows\flutter_local_notifications_plugin.cpp(224): warning C4715: '`anonymous namespace'::FlutterLocalNotificationsPlugin::Initialize': not all control paths return a value [C:\Users\test\Projects\Side-projects\tomatoro\tomatoro-app\build\windows\plugins\flutter_localBuild process failed.
 
#0      WindowsBuild.build (package:msix/src/windowsBuild.dart:33:7)
<asynchronous suspension>
#1      Msix._createMsix (package:msix/msix.dart:73:7)
<asynchronous suspension>
#2      Msix.create (package:msix/msix.dart:33:5)
<asynchronous suspension>
#3      main (file:///C:/Users/test/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/msix-3.3.1/bin/create.dart:4:3)
<asynchronous suspension>
pub finished with exit code 255

sidevesh avatar Apr 18 '22 16:04 sidevesh

Thanks for reporting, it's an easy fix.

kennethnym avatar Apr 19 '22 11:04 kennethnym

@sidevesh the issue should be fixed.

kennethnym avatar Apr 19 '22 11:04 kennethnym

Hey @kennethnym , after the latest commit, I am able to build the app but notification has stopped showing up now. I had actually made a simple patch earlier replacing the CreateToastNotifier with the correct version and had gotten it to work with the app name showing up correctly, so we should be close to a solution. I encountered another issue which is that on clicking the notification it opens another copy of the app rather than bring the app to focus or dismiss. In macOS and Linux, clicking the notification without any on SelectNotification just dismisses the notification and brings the app to focus and I am trying to replicate that on Windows. Is it possible to implement that behavior ?

sidevesh avatar Apr 25 '22 06:04 sidevesh

after the latest commit, I am able to build the app but notification has stopped showing up now.

I had actually made a simple patch earlier replacing the CreateToastNotifier with the correct version and had gotten it to work with the app name showing up correctly, so we should be close to a solution.

I encountered another issue which is that on clicking the notification it opens another copy of the app rather than bring the app to focus or dismiss.

Thanks for reporting, I'll look into this.

In macOS and Linux, clicking the notification without any on SelectNotification just dismisses the notification and brings the app to focus and I am trying to replicate that on Windows.

It should be possible to implement this behavior on Windows as well.

kennethnym avatar Apr 26 '22 18:04 kennethnym

@sidevesh From the issue you linked earlier, @azchohfi mentioned:

For deciding whether to use the overloaded method... HasIdentity true = CreateToastNotifier() HasIdentity false = CreateToastNotifier(myAumid)"

I have in fact integrated this into the code in previous commits already:

if (hasIdentity.value())
    toastNotifier = winrt::Windows::UI::Notifications::ToastNotificationManager::CreateToastNotifier();
else
    toastNotifier = winrt::Windows::UI::Notifications::ToastNotificationManager::CreateToastNotifier(winrt::to_hstring(aumid));

Is the app name still not showing up correctly?

As for the other issues, I still need some time to figure out what's going on that is causing them to occur.

kennethnym avatar May 04 '22 17:05 kennethnym

hey @kennethnym , tried it again and same behavior, notifications have stopped showing up, so can't really see if the app name in notification is fixed or not.

sidevesh avatar May 14 '22 13:05 sidevesh

Like I mentioned previously I had simply replaced the CreateToastNotifier with the correct version and managed to fix the app name, although that would mess up non msix apps so your fix with the check built in is the proper solution here.

sidevesh avatar May 14 '22 13:05 sidevesh

I am struggling to understand why it would stop working altogether. Exams are coming up for me; I'll start working on it again after exams.

kennethnym avatar May 14 '22 13:05 kennethnym

Also, any pointers about the issue: on clicking the notification it opens another copy of the app rather than bring the app to focus or dismiss ?

sidevesh avatar May 14 '22 13:05 sidevesh

I have no idea what is causing that issue either. It wasn't happening before, and I never changed the code related to that.

kennethnym avatar May 14 '22 13:05 kennethnym

I have no idea what is causing that issue either. It wasn't happening before, and I never changed the code related to that.

Okay, I will try testing this in a non msix build to see if msix packaging is causing the difference in behavior, might help us debug this issue.

sidevesh avatar May 14 '22 13:05 sidevesh

I'm looking forward to using this on Windows, Is it a long way from launch?

DomingoMG avatar Jun 12 '22 13:06 DomingoMG

Sorry for the silence. I've been very busy lately, so I was not able to squeeze time to work on this. I will give this another look over the weekend.

kennethnym avatar Jul 04 '22 07:07 kennethnym