Mac App: Push Notifications Arriving, but Not Showing Up in Notification Center

474 Views Asked by At

I have a question about push notifications on macOS. I am successfully getting notifications in my Mac app (I can see them hit didReceiveRemoteNotification in my app delegate). But nothing actually shows up in Notification Center.

The same notification shows up fine in my iOS version of the same app, so I'm pretty sure my push notification is formed correctly.

With macOS, do I have to take the content of the push and deliver it in order for it to show up?

On iOS, I just have to form the push notification with certain parameters and it will be delivered as a visible push notification (as opposed to a silent background notification). I'm wondering if the same isn't true for macOS (10.13).

Here's my app delegate code:

class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {

  func applicationDidFinishLaunching(_ aNotification: Notification) {
      NSUserNotificationCenter.default.delegate = self
  }

  func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
    let dict = userInfo as! [String: NSObject]
    let notification = CKNotification(fromRemoteNotificationDictionary: dict)

    if let sub = notification.subscriptionID{
      print("Notification: \(sub)") //<-- This fires every time a notification arrives
    }
  }

  //Make sure the notification shows up even if the app is active
  func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
    return true
  }

  //??? - Do I need something like this? - ???
  func userNotificationCenter(_ center: NSUserNotificationCenter, didDeliver notification: NSUserNotification) {
    NSUserNotificationCenter.default.deliver(notification)
  }
}
1

There are 1 best solutions below

0
On

It turns out that when I was registering for notifications, I was doing this:

NSApplication.shared.registerForRemoteNotifications(matching: [])

This allowed the silent background notifications to come in, but I was omitting the alert types that allow user push notifications to arrive.

This fixed it:

NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])