Notifications with sound

I just noticed that Local Notifications in a Xojo made app do not have any sound.
This is because the underlying iOS class, UNMutableNotificationContent does not have a default value for its sound property.

To fix this, add the following method in a module, or use iOSDesignExtensions:

Public Sub UseDefaultSoundXC(extends content As NotificationContent)
  //new in version 2.0.1
  
  Declare Function defaultSound lib "UserNotifications" selector "defaultSound" (obj as ptr) as ptr
  declare function NSClassFromString lib "Foundation" (clsName as CFStringRef) as ptr
  
  declare sub setSound lib "UserNotifications" Selector "setSound:" (obj as ptr, snd as ptr)
  
  setSound(content.Handle, defaultSound(NSClassFromString("UNNotificationSound")))
End Sub

When setting up a new local notification, add one line like this:

//Creating a new notification
Dim content As new NotificationContent("Title of notification", "", "The body")

content.UseDefaultSoundXC //Just add this line to enable a sound

//Send notification in 20 seconds
NotificationCenter.Send(20, content) 
6 Likes