macOS 远程推送通知不显示警报横幅
macOS Remote Push Notifications Not Showing Alert Banner
我正在尝试为我的 macOS 应用程序启用推送通知。一切似乎都在运作。我能够获得设备令牌。发送通知没有错误。除了我的 Mac.
上没有显示警报
我添加了以下代码以查看我的应用程序是否收到它。
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
print(userInfo)
}
发送通知后,我在控制台中看到以下内容。
["aps": {
alert = "Alert - Hello World";
sound = "ping.aiff";
}]
看来它可以正常到达设备,只是没有显示警报。
我在 iOS 上测试了完全相同的设置,它工作正常并在那里显示警报。所以我一定是在 macOS 上遗漏了一些东西。
我尝试了以下方法来解决这个问题:
- 在关闭和打开应用程序的情况下进行测试(两次均无效)
- 确保在系统偏好设置中为应用程序启用通知
- 如果我在代码中手动创建本地通知,它会完美运行,并且会出现通知横幅
- 我无法在旧版本的 macOS 上测试它,因为我正在使用的推送通知 API 刚刚在 macOS Mojave
中发布
- 我也试过创建另一个测试项目,也出现了同样的问题
- 我已确保“请勿打扰”已关闭,并已在通知中心查看通知,但也没有显示。
如何让它在 macOS 上显示横幅并播放声音?
在 iOS 中,以下代码工作正常:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
对于 macOS,我将该代码更改为:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
NSApplication.shared.registerForRemoteNotifications()
}
}
原来 NSApplication.shared.registerForRemoteNotifications()
行不正确。在 macOS 上,您必须传入您在此调用中提供的相同选项。
将该行更改为以下内容有效。
NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])
我觉得奇怪的是,在 Apple's documentation 中它说该方法已被弃用,我们应该改用 registerForRemoteNotifications()
。这让我觉得 registerForRemoteNotifications()
存在某种类型的错误,通知显示不正确。
还有一件事要提。确实花了一点时间(几分钟),并发送了一些通知,它们在进行更改后才真正出现。不确定这是否只是由于互联网连接速度慢或其他原因。但是现在发送后出现的速度非常快
编辑
Apple 已通知我此问题已在 macOS 10.14.4 中修复。我还不能升级到最好的并测试它。所以我现在无法确认。当我有机会在 macOS 10.14.4 上测试时,我会更新这个。
澄清一下,这是 macOS,而 NSApp 只是 NSApplication.shared
实际上我必须修改我的答案,因为我确实有一些不一致的结果,所以现在,我确实有
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])
}
我正在尝试为我的 macOS 应用程序启用推送通知。一切似乎都在运作。我能够获得设备令牌。发送通知没有错误。除了我的 Mac.
上没有显示警报我添加了以下代码以查看我的应用程序是否收到它。
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
print(userInfo)
}
发送通知后,我在控制台中看到以下内容。
["aps": {
alert = "Alert - Hello World";
sound = "ping.aiff";
}]
看来它可以正常到达设备,只是没有显示警报。
我在 iOS 上测试了完全相同的设置,它工作正常并在那里显示警报。所以我一定是在 macOS 上遗漏了一些东西。
我尝试了以下方法来解决这个问题:
- 在关闭和打开应用程序的情况下进行测试(两次均无效)
- 确保在系统偏好设置中为应用程序启用通知
- 如果我在代码中手动创建本地通知,它会完美运行,并且会出现通知横幅
- 我无法在旧版本的 macOS 上测试它,因为我正在使用的推送通知 API 刚刚在 macOS Mojave 中发布
- 我也试过创建另一个测试项目,也出现了同样的问题
- 我已确保“请勿打扰”已关闭,并已在通知中心查看通知,但也没有显示。
如何让它在 macOS 上显示横幅并播放声音?
在 iOS 中,以下代码工作正常:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
对于 macOS,我将该代码更改为:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
NSApplication.shared.registerForRemoteNotifications()
}
}
原来 NSApplication.shared.registerForRemoteNotifications()
行不正确。在 macOS 上,您必须传入您在此调用中提供的相同选项。
将该行更改为以下内容有效。
NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])
我觉得奇怪的是,在 Apple's documentation 中它说该方法已被弃用,我们应该改用 registerForRemoteNotifications()
。这让我觉得 registerForRemoteNotifications()
存在某种类型的错误,通知显示不正确。
还有一件事要提。确实花了一点时间(几分钟),并发送了一些通知,它们在进行更改后才真正出现。不确定这是否只是由于互联网连接速度慢或其他原因。但是现在发送后出现的速度非常快
编辑
Apple 已通知我此问题已在 macOS 10.14.4 中修复。我还不能升级到最好的并测试它。所以我现在无法确认。当我有机会在 macOS 10.14.4 上测试时,我会更新这个。
澄清一下,这是 macOS,而 NSApp 只是 NSApplication.shared
实际上我必须修改我的答案,因为我确实有一些不一致的结果,所以现在,我确实有
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])
}