Play Push Notification Sound effect when the application is down (Mac)

I am adding Push Notifications to my Mac application. Notifications are displayed very well, but for some reason I can’t get the sound effects to play the way I think they should.

I pass custom sound to play "custom.caf" in apns payload. This works currently for our iOS app. It does not play anything when it enters the Mac application. Delving into Apple’s very limited documentation, the PushyMac app shows that you must play the sound manually based on the userInfo dictionary that comes in to click. If I play the sound manually while the application is running, I get a sound effect.

My problem is that the application is closed and not working. I still get push notifications as expected, but no sounds are played. From what I can say, I'm just screwed. I have other Mac apps that still manage to play sounds when they don't work. In particular, my email application, Spark. Therefore, I know that this is possible, but I can’t understand what kind of secret sauce it is. Please help !!

+4
source share
2 answers

It is very easy to implement custom sound ability for Apple Push Notifications; the problem is that the documentation does not mention what is required for this.

, , - (, Today Extension). , , APN. , , ( NotificationCenter.framework), .

> :

today extension

Extension > > Bundle > :

build phases

APN, , :

func applicationDidFinishLaunching(_ aNotification: Notification) {
                                   NSApplication.shared().
                                   registerForRemoteNotifications(matching: 
                                   [.alert, .sound, .badge])
                                   NSApp.dockTile.badgeLabel = nil
}

func application(_ application: NSApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
}

func application(_ application: NSApplication, didReceiveRemoteNotification 
                                userInfo: [String : Any]) {
}

func application(_ application: NSApplication, 
                 didFailToRegisterForRemoteNotificationsWithError error: Error) {
                 print("could not register for notfications: \(error)")
}

, , , AudioServicesPlaySystemSound, , , , NotificationCenter.framework , .

+1

Mac OS X Catelina! "custom.caf" , "custom.wav" Mac OS! iOS !

0

Source: https://habr.com/ru/post/1665352/


All Articles