Reputation: 20526
I'm trying to enable Push Notifications for my macOS application. Everything seems to be working. I'm able to get the device token. Send the notification without errors. Except there is no alert that shows up on my Mac.
I added the following code to see if my application was receiving it or not.
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
print(userInfo)
}
And after I send a notification I see the following in the console.
["aps": {
alert = "Alert - Hello World";
sound = "ping.aiff";
}]
So it looks like it's getting to the device ok, just not showing the alert.
I have tested the exact same setup on iOS and it's working fine and showing the alert there. So I must be missing something specifically on macOS.
I have tried the following things to fix this:
How can I get it to show the banner and play the sound on macOS?
Upvotes: 2
Views: 2397
Reputation: 20526
In iOS the following code works fine:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
For macOS I changed that code to be:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
NSApplication.shared.registerForRemoteNotifications()
}
}
Turns out the line NSApplication.shared.registerForRemoteNotifications()
is incorrect. On macOS you have to pass in the same options you provided into this call.
Changing that line to the following worked.
NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])
What I find strange is that in Apple's documentation it says that method is deprecated, and that we should use registerForRemoteNotifications()
instead. Which makes me think there is some type of bug with registerForRemoteNotifications()
where the notifications are not appearing correctly.
One other thing to mention. It did take a little bit of time (couple minutes), and a few notifications sent, for them to actually appear after making that change. Not sure if it was just due to slow internet connection or what. But now they are appearing very quickly after being sent.
Edit
Apple has informed me that this is fixed in macOS 10.14.4. I have not been able to upgrade to the best and test it yet tho. So I can not confirm at this point. I will update this when I get a chance to test on macOS 10.14.4.
Upvotes: 3
Reputation: 1
Clarifying, this is macOS, and NSApp is just NSApplication.shared
Actually I have to amend my answer because I did have some inconsistent results, so right now, I do have
if #available(OSX 10.14, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { [weak self] (success, error) in
if let error = error {
// LOG
} else {
NSApp.registerForRemoteNotifications(matching: [.alert, .badge, .sound])
UNUserNotificationCenter.current().setNotificationCategories(varWithMyCategories)
}
}
} else {
NSApp.registerForRemoteNotifications(matching: [.alert, .badge, .sound])
}
Upvotes: -1