Skip to content

Commit f4fd75c

Browse files
committed
Minimal local toast example
using JNI
1 parent 5aee184 commit f4fd75c

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

integrations-win.vcxproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
44
<ProjectConfiguration Include="Debug|Win32">
@@ -117,6 +117,7 @@
117117
<ClInclude Include="src\main\headers\org_cryptomator_windows_autostart_WinShellLinks_Native.h" />
118118
<ClInclude Include="src\main\headers\org_cryptomator_windows_keychain_WinDataProtection_Native.h" />
119119
<ClInclude Include="src\main\headers\org_cryptomator_windows_keychain_WindowsHello_Native.h" />
120+
<ClInclude Include="src\main\headers\org_cryptomator_windows_notify_WinToasts_Native.h" />
120121
<ClInclude Include="src\main\headers\org_cryptomator_windows_uiappearance_WinAppearance_Native.h" />
121122
<ClInclude Include="src\main\resources\ktmw32_helper.h" />
122123
</ItemGroup>
@@ -125,6 +126,10 @@
125126
<ClCompile Include="src\main\native\org_cryptomator_windows_keychain_WinDataProtection_Native.cpp" />
126127
<ClCompile Include="src\main\native\org_cryptomator_windows_keychain_WindowsHello_Native.cpp" />
127128
<ClCompile Include="src\main\native\org_cryptomator_windows_uiappearance_WinAppearance_Native.cpp" />
129+
<ClCompile Include="src\main\native\org_cryptomator_windows_notify_WinToasts_Native.cpp" />
130+
</ItemGroup>
131+
<ItemGroup>
132+
<None Include="Makefile" />
128133
</ItemGroup>
129134
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
130135
<ImportGroup Label="ExtensionTargets">

src/main/headers/org_cryptomator_windows_notify_WinToasts_Native.h

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.cryptomator.windows.notify;
2+
3+
import org.cryptomator.windows.common.NativeLibLoader;
4+
import org.cryptomator.windows.common.WinStrings;
5+
6+
class WinToasts {
7+
8+
9+
/**
10+
* Sends an app notification to Windows.
11+
*
12+
* @param appUserModelId Application User Model ID associated with the notification
13+
* @param toastXml xml string describing the toast message
14+
* @return {@code 0} if everything worked, otherwise an HRESULT error code
15+
*/
16+
public int sendToastNotification(String appUserModelId, String toastXml) {
17+
return Native.INSTANCE.sendToastNotification(
18+
WinStrings.getNullTerminatedUTF16Representation(appUserModelId),
19+
WinStrings.getNullTerminatedUTF16Representation(toastXml));
20+
}
21+
22+
private static class Native {
23+
static final Native INSTANCE = new Native();
24+
25+
private Native() {
26+
NativeLibLoader.loadLib();
27+
}
28+
29+
native int sendToastNotification(byte[] AppUserModelId, byte[] storagePath);
30+
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.cryptomator.windows.notify;
2+
3+
public class WindowsNotifications {
4+
5+
private static final String WINDOWS_NOTIFIATION_AUMID_PROPERTY = "cryptomator.integrationsWin.aumid";
6+
private static final String XML_TEMPLATE = """
7+
<toast>
8+
<visual>
9+
<binding template='ToastGeneric'>
10+
<text>Cryptomator says Hello</text>
11+
</binding>
12+
</visual>
13+
<actions>
14+
<action content='Call back Cryptomator' arguments='the_args' activationKind='Foreground' />
15+
</actions>
16+
</toast>""";
17+
18+
private final WinToasts proxy;
19+
20+
public WindowsNotifications() {
21+
proxy = new WinToasts();
22+
}
23+
24+
public void sendNotification() {
25+
var aumid = System.getProperty(WINDOWS_NOTIFIATION_AUMID_PROPERTY, "Cryptomator.Cryptomator");
26+
proxy.sendToastNotification(aumid,XML_TEMPLATE);
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <windows.h>
3+
#include <string_view>
4+
#include <winrt/Windows.UI.Notifications.h>
5+
#include <winrt/Windows.Data.Xml.Dom.h>
6+
#include "org_cryptomator_windows_notify_WinToasts_Native.h"
7+
8+
using namespace winrt;
9+
using namespace Windows::Data::Xml::Dom;
10+
using namespace Windows::UI::Notifications;
11+
12+
JNIEXPORT jint JNICALL Java_org_cryptomator_windows_notify_WinToasts_00024Native_sendToastNotification
13+
(JNIEnv* env, jobject obj, jbyteArray jAumid, jbyteArray jToastXml) {
14+
LPCWSTR aumid = (LPCWSTR)env->GetByteArrayElements(jAumid, NULL);
15+
LPCWSTR toastXmlString = (LPCWSTR)env->GetByteArrayElements(jToastXml, NULL);
16+
HRESULT result = 0;
17+
winrt::init_apartment();
18+
try {
19+
std::wstring wAUMID{ aumid };
20+
std::wstring wToastXmlString{ toastXmlString };
21+
XmlDocument toastXml;
22+
toastXml.LoadXml(wToastXmlString);
23+
ToastNotification toast{ toastXml };
24+
toast.ExpiresOnReboot(true);
25+
ToastNotifier notifier{ ToastNotificationManager::CreateToastNotifier(wAUMID) };
26+
notifier.Show(toast);
27+
}
28+
catch (winrt::hresult_error const& e) {
29+
std::wcout << std::wstring_view( e.message()) << std::endl;
30+
result = e.code();
31+
}
32+
env->ReleaseByteArrayElements(jAumid, (jbyte*) aumid, JNI_ABORT);
33+
env->ReleaseByteArrayElements(jToastXml, (jbyte*) toastXmlString, JNI_ABORT);
34+
return result;
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.cryptomator.windows.notify;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
public class WinToastsTest {
6+
7+
@Test
8+
public void sendToast() {
9+
var toaster = new WindowsNotifications();
10+
11+
toaster.sendNotification();
12+
}
13+
}

0 commit comments

Comments
 (0)