Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/User Notification/Send Notification with UNNotification
Required plugins for this example: MBS Util Plugin, MBS MacFrameworks Plugin, MBS MacBase Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/User Notification/Send Notification with UNNotification
This example is the version from Thu, 4th Nov 2020.
Project "Send Notification with UNNotification.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
'Register MBS plugins here.
#If XojoVersion >= 2020 Then
// API 2.0
'Code signing is required to use UNUserNotificationCenter, unlike with NSNotifications
'Add a signing build script to run in the debugger. Otherwise just see the error messages :-)
if SystemInformationMBS.isMojave then
myNotificationCenter = new theCenter
if myNotificationCenter<>nil then
myNotificationCenter.requestAuthorization(7)
Window1.Show
else
MessageBox "Notification Center not found. So this is an exercise in futility."
Quit
end if
else
// in a real project you might continue to use NSNotifications if you get here.
//NSNotification was deprecated in Mojave.
MessageBox "This example will run on macOS 10.15.x 'Mojave' or greater."
Quit
end if
Exception
MessageBox "Something went wrong. Who knows?"
Quit
#Else
// API 1.0
'Code signing is required to use UNUserNotificationCenter, unlike with NSNotifications
'Add a signing build script to run in the debugger. Otherwise just see the error messages :-)
If SystemInformationMBS.isMojave Then
myNotificationCenter = New theCenter
If myNotificationCenter<>Nil Then
myNotificationCenter.requestAuthorization(7)
Window1.Show
Else
MsgBox "Notification Center not found. So this is an exercise in futility."
Quit
End If
Else
// in a real project you might continue to use NSNotifications if you get here.
//NSNotification was deprecated in Mojave.
msgbox "This example will run on macOS 10.15.x 'Mojave' or greater."
Quit
End If
Exception
MsgBox "Something went wrong. Who knows?"
Quit
#EndIf
End EventHandler
Property myNotificationCenter As theCenter
End Class
Class Window1 Inherits Window
Control ourTitle Inherits TextField
ControlInstance ourTitle Inherits TextField
End Control
Control ourBody Inherits TextField
ControlInstance ourBody Inherits TextField
End Control
Control ourSubtitle Inherits TextField
ControlInstance ourSubtitle Inherits TextField
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control GroupBox1 Inherits GroupBox
ControlInstance GroupBox1 Inherits GroupBox
End Control
Control nowButton Inherits PushButton
ControlInstance nowButton Inherits PushButton
EventHandler Sub Action()
'On Mojave and Catalina, we could send the notification with zero-delay.
'For Big Sur we had to put the slight delay in, to get the notification to appear with the app in front.
sendIt(.1)
End EventHandler
End Control
Control timerButton Inherits PushButton
ControlInstance timerButton Inherits PushButton
EventHandler Sub Action()
sendIt(5)
End EventHandler
End Control
Control statusLabel Inherits Label
ControlInstance statusLabel Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
'Let's clear the statusLabel in case we want to repeat the exercise.
#If XojoVersion >= 2020 Then
// API 2.0
If System.Ticks >= startTime+300 Then
statusLabel.Text = ""
Me.RunMode = Timer.RunModes.Off
End If
#Else
// API 1.0
If Ticks >= startTime+300 Then
statusLabel.Text = ""
Me.Mode = Timer.ModeOff
End If
#EndIf
End EventHandler
End Control
EventHandler Sub Close()
Quit
End EventHandler
Sub sendIt(theInterval as Double)
#If XojoVersion >= 2020 Then
// API 2.0
'Our notifications will not be authorized unless we're code-signed. So either sign a build or, to debug, insert an appropriate build script.
if UNUserNotificationCenterMBS.Available then
Var myNotification as new UNMutableNotificationContentMBS()
myNotification.title = ourTitle.Value
myNotification.subtitle = ourSubtitle.Value
myNotification.body = ourBody.value
Var myTrigger as UNTimeIntervalNotificationTriggerMBS
myTrigger = New UNTimeIntervalNotificationTriggerMBS(theInterval,False)
Var myRequest as new UNNotificationRequestMBS(app.ExecutableFile.Name, myNotification,myTrigger)
' On Mojave and Catalina we could pass nil for the trigger, when sending the "immediate" option. But we found we couldn't on Big Sur.
' Also, we cannot zero for the trigger, because it will throw an NSException. So we settled for .1
app.myNotificationCenter.addNotificationRequest(myRequest,nil)
else
MessageBox "not available"
end if
#Else
'Our notifications will not be authorized unless we're code-signed. So either sign a build or, to debug, insert an appropriate build script.
If UNUserNotificationCenterMBS.Available Then
Dim myNotification As New UNMutableNotificationContentMBS
myNotification.title = ourTitle.Text
myNotification.subtitle = ourSubtitle.Text
myNotification.body = ourBody.Text
Dim myTrigger As UNTimeIntervalNotificationTriggerMBS
myTrigger = New UNTimeIntervalNotificationTriggerMBS(theInterval, False)
Dim myRequest As New UNNotificationRequestMBS(app.ExecutableFile.Name, myNotification,myTrigger)
' On Mojave and Catalina we could pass nil for the trigger, when sending the "immediate" option. But we found we couldn't on Big Sur.
' Also, we cannot zero for the trigger, because it will throw an NSException. So we settled for .1
app.myNotificationCenter.addNotificationRequest(myRequest,Nil)
Else
msgbox "not available"
End If
#EndIf
End Sub
Note "About"
This example was provided by Jerome Fritschle.
Thanks
Property startTime As Uint64
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class theCenter Inherits UNUserNotificationCenterMBS
EventHandler Sub addNotificationRequestCompleted(request as UNNotificationRequestMBS, error as NSErrorMBS, tag as variant)
#Pragma Unused tag
#If XojoVersion >= 2020 Then
// API 2.0
Var s As String
if error<>nil then
' Will wind up here if not signed.
s = error.Domain+" "+error.LocalizedDescription
else
s = "Message delivered! Did it display?"
end if
Window1.statusLabel.Text = s
Window1.startTime = System.Ticks
Window1.Timer1.RunMode = Timer.RunModes.Multiple
#Else
// API 1.0
Dim s As String
If error<>Nil Then
' Will wind up here if not signed.
s = error.Domain+" "+error.LocalizedDescription
Else
s = "Message delivered! Did it display?"
End If
Window1.statusLabel.Text = s
Window1.startTime = Ticks
Window1.Timer1.mode = Timer.ModeMultiple
#EndIf
End EventHandler
EventHandler Sub requestAuthorizationCompleted(granted as boolean, error as NSErrorMBS, tag as variant)
#Pragma Unused tag
#If XojoVersion >= 2020 Then
// API 2.0
Var s As String
if error<>nil then
' We're probably not code-signed
s = "Error code "+error.code.ToString+". Authorization refused. Are we code-signed?"
else
s = "Authorized by Notification Center!"
end if
Window1.statusLabel.Text = s
#Else
// API 1.0
Dim s As String
If error<>Nil Then
' We're probably not code-signed
s = "Error code " + str(error.code) + ". Authorization refused. Are we code-signed?"
Else
s = "Authorized by Notification Center!"
End If
Window1.statusLabel.Text = s
#EndIf
End EventHandler
EventHandler Sub willPresentNotification(notification as UNNotificationMBS, byref options as Integer)
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacFrameworks Plugin.