flutter_local_notifications
flutter_local_notifications copied to clipboard
[flutter_local_notifications] [WIP] Windows implementation of notifications
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
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
I tried using the default templates but it would just show "New notification" instead of the actual body.
@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:
title
is 'plain title'
, but it is not shown in the notification.
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:
Let me know if this helps!
Ahh thanks, that makes sense now. I am not a C++ programmer, so I really appreciate your help!
Hi, is it working as expected now after your latest commits?
Basic title body notification is working, but nothing beyond that.
@lzzy12 Just an update: I have implemented cancel
and cancelAll
in the latest commit.
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
@kennethnym any updates on this PR?
Hi sorry, busy with coursework at the moment, I will continue the work this weekend.
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.
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.
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.
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
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 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.
Thanks for bring that to attention, I'll investigate how to handle msix apps as well.
@sidevesh Can you test if the latest commit fixes your issue?
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
Thanks for reporting, it's an easy fix.
@sidevesh the issue should be fixed.
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 ?
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.
@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.
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.
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.
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.
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
?
I have no idea what is causing that issue either. It wasn't happening before, and I never changed the code related to that.
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.
I'm looking forward to using this on Windows, Is it a long way from launch?
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.