我对 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)
}
}