我对macOS上的推送通知有疑问。我在Mac应用程序中成功收到了通知(我可以在应用程序委托中看到它们按didReceiveRemoteNotification
)。但是实际上什么都没有显示在通知中心。
同一通知在我的同一应用的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)
}
}
答案 0 :(得分:1)
事实证明,当我注册通知时,我正在这样做:
NSApplication.shared.registerForRemoteNotifications(matching: [])
这允许进入静默后台通知,但是我省略了允许用户推送通知到达的警报类型。
此问题已解决:
NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])