1

我对 macOS 上的推送通知有疑问。我在我的 Mac 应用程序中成功获得了通知(我可以didReceiveRemoteNotification在我的应用程序委托中看到它们)。但是Notification Center 中实际上没有显示任何内容

相同的通知在我的同一应用程序的 iOS 版本中显示得很好,所以我很确定我的推送通知格式正确。

对于 macOS,我是否必须获取推送的内容deliver才能显示出来?

在 iOS 上,我只需要使用某些参数形成推送通知,它将作为可见的推送通知传递(而不是静默的背景通知)。我想知道macOS(10.13)是否也是如此。

这是我的应用程序委托代码:

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)
  }
}
4

1 回答 1

1

事实证明,当我注册通知时,我正在这样做:

NSApplication.shared.registerForRemoteNotifications(matching: [])

这允许静默背景通知进入,但我忽略了允许用户推送通知到达的警报类型。

这修复了它:

NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])
于 2018-07-30T21:39:59.453 回答