B4J Question Any established way for B4J to produce notifications for macOS/Win10?

Sandman

Expert
Licensed User
I'm curious if anyone has made code to produce notifications on macOS and/or Windows?

I believe it's called Notification Center on macOS and Action Center on Windows.

You know, like these:
1606334748400.png
 

Sandman

Expert
Licensed User
Interesting, thanks. And that gave me a clue on what to search for when it comes to Windows, which led to these:
And for posterity, here's roughly how to produce a notification on macOS:
B4X:
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

And the same on Windows:
B4X:
new ToastContentBuilder()
    .AddToastActivationInfo("app-defined-string", ToastActivationType.Foreground)
    .AddText("Some text")
    .AddButton("Archive", ToastActivationType.Background, "archive")
    .AddAudio(new Uri("ms-appx:///Sound.mp3"));
 
Upvote 0

Magma

Well-Known Member
Licensed User
Longtime User
Interesting, thanks. And that gave me a clue on what to search for when it comes to Windows, which led to these:
And for posterity, here's roughly how to produce a notification on macOS:
B4X:
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

And the same on Windows:
B4X:
new ToastContentBuilder()
    .AddToastActivationInfo("app-defined-string", ToastActivationType.Foreground)
    .AddText("Some text")
    .AddButton("Archive", ToastActivationType.Background, "archive")
    .AddAudio(new Uri("ms-appx:///Sound.mp3"));

Hi there... did you find a way for Windows Notifications ?
 
Upvote 0

Magma

Well-Known Member
Licensed User
Longtime User
Found a simple batch/powershell script... trying to get more...

save it as .bat (ballon.bat for example)...
go to cmd line and run it to see how works..
B4X:
@echo off

if "%~1"=="" call :printhelp  & exit /b 1
setlocal

if "%~2"=="" (set Icon=Information) else (set Icon=%2)
powershell -Command "[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); $objNotifyIcon=New-Object System.Windows.Forms.NotifyIcon; $objNotifyIcon.BalloonTipText='%~1'; $objNotifyIcon.Icon=[system.drawing.systemicons]::%Icon%; $objNotifyIcon.BalloonTipTitle='%~3'; $objNotifyIcon.BalloonTipIcon='None'; $objNotifyIcon.Visible=$True; $objNotifyIcon.ShowBalloonTip(5000);"

endlocal
goto :eof

:printhelp
echo USAGE: %~n0 Text [Icon [Title]]
echo Icon can be: Application, Asterisk, Error, Exclamation, Hand, Information, Question, Shield, Warning or WinLogo
exit /b

example of cmd line to check it:
balloon.bat "test" Error "test"

only problem is getting that is running from powershell...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    xui.SetDataFolder("MyApp")
    File.Copy(File.DirAssets, "notification.bat", xui.DefaultFolder, "notification.bat")
End Sub

Sub ShowNotification (Body As String, Title As String)
    Dim shl As Shell
    shl.Initialize("shl", "cmd.exe", Array("/c", File.Combine(xui.DefaultFolder, "notification.bat"), Body, "Information", Title))
    shl.Run(10000)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(Success)
End Sub

only problem is getting that is running from powershell...
It is probably impossible, but it can still be useful.
 
Last edited:
Upvote 0

Magma

Well-Known Member
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    xui.SetDataFolder("MyApp")
    File.Copy(File.DirAssets, "notification.bat", xui.DefaultFolder, "notification.bat")
End Sub

Sub ShowNotification (Body As String, Title As String)
    Dim shl As Shell
    shl.Initialize("shl", "cmd.exe", Array("/c", File.Combine(xui.DefaultFolder, "notification.bat"), Body, "Information", Title))
    shl.Run(10000)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(Success)
End Sub


It is probably impossible, but it can still be useful.
https://eddiejackson.net/wp/?p=18877

You have to look... that... will help you create a "future" library for windows 10 notifications!!!! without the powershell ISE title ...

scroll down and go at: More Advanced Toast (yes, this is the toast you’re looking for) - check the example Toast1.ps

May be the best toast for windows 10 :)
 
Upvote 0

Sandman

Expert
Licensed User
I stumbled on some more relevant links for Windows 10 (and one for Mac) and thought I'd add them here in case somebody finds this thread and wants to give it a go to bring it all into B4J. :)
 
Upvote 0

Daestrum

Well-Known Member
Licensed User
Longtime User
This will generate a windows notification
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI 
    Private Button1 As B4XView
    Dim img As Image
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    img.Initialize("c:/temp","icontest.png")
End Sub

Sub Button1_Click
    asJO(Me).RunMethod("displayTray",Array(img,"test1","test text"))    
End Sub

Sub asJO (o As JavaObject) As JavaObject
    Return o
End Sub
#if java
import java.awt.*;
import java.awt.event.*;
import java.awt.TrayIcon.MessageType;
import java.net.MalformedURLException;
import javafx.embed.swing.SwingFXUtils;

public static void displayTray(javafx.scene.image.Image img,String title,String msg) throws AWTException, MalformedURLException {
    SystemTray tray = SystemTray.getSystemTray();
    TrayIcon trayIcon = new TrayIcon(SwingFXUtils.fromFXImage(img, null));
    tray.add(trayIcon);
    trayIcon.displayMessage(title,msg, MessageType.INFO);
}
#End If

1623923271865.png
 
Upvote 0

Magma

Well-Known Member
Licensed User
Longtime User
is it possible to remove OpenJDK Platform Library ???? or replace it with custom text someway?
 
Upvote 0

Sandman

Expert
Licensed User
This will generate a windows notification
Very cool! You seem to have a nice solution there, I have a some follow-up questions:

It didn't use the png I specified, instead it showed the (i)-icon that you have in your screenshot.

When I clicked it, it just disappeared. Is there some reasonable way to launch an app, or a specified webpage?

Is it possible to replace the "OpenJDK Platform binary" with something else, like the name of the binary I just compiled?
 
Upvote 0

Daestrum

Well-Known Member
Licensed User
Longtime User
The "OpenJDK Platform binary" is shown because it's java issuing the notification. Not sure if it can be changed.

The image is for the system tray icon - not the message, that uses the apps icon in this case java.
 
Upvote 0

Sandman

Expert
Licensed User
Yeah, I'm thinking that this probably is because we're sending something into the old-skool systemtray, which is picked up in Windows 10 and instead shown in the notification center. So with this code we're not getting the full benefits of the new notification center features.

Could that be correct?
 
Upvote 0

Daestrum

Well-Known Member
Licensed User
Longtime User
Probably yes, not really played too much with notifications. On the upside it will work in both UI and Non-UI apps as it uses awt.
 
Upvote 0
Top