【问题标题】:macOS Remote Push Notifications Not Showing Alert BannermacOS 远程推送通知不显示警报横幅
【发布时间】:2019-04-07 17:37:30
【问题描述】:

我正在尝试为我的 macOS 应用程序启用推送通知。一切似乎都在工作。我能够获得设备令牌。发送没有错误的通知。除非我的 Mac 上没有显示警报。

我添加了以下代码来查看我的应用程序是否收到它。

func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
    print(userInfo)
}

在我发送通知后,我会在控制台中看到以下内容。

["aps": {
    alert = "Alert - Hello World";
    sound = "ping.aiff";
}]

所以看起来它可以到达设备,只是没有显示警报。

我已经在 iOS 上测试了完全相同的设置,它工作正常并在那里显示警报。所以我一定是在 macOS 上遗漏了一些东西。

我尝试了以下方法来解决此问题:

  1. 在应用程序关闭和打开的情况下进行了测试(两次均无效)
  2. 确保在系统偏好设置中为应用程序启用通知

  1. 如果我在代码中手动创建本地通知,它可以完美运行,并且出现通知横幅
  2. 我无法在旧版本的 macOS 上测试它,因为我使用的推送通知 API 刚刚在 macOS Mojave 中发布
  3. 我也尝试创建另一个测试项目,同样的问题发生了
  4. 我已确保“请勿打扰”已关闭,并已在通知中心检查了该通知,但它也没有显示在那里。

如何让它在 macOS 上显示横幅并播放声音?

【问题讨论】:

标签: swift macos push-notification apple-push-notifications


【解决方案1】:

在 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 上进行测试时,我会更新此内容。

【讨论】:

  • > 这让我觉得 registerForRemoteNotifications() 存在某种类型的错误,通知显示不正确。 我同意
  • @erkanyildiz 您是否遇到同样的问题?
  • 是的。在UNUserNotificationCenter 上调用requestAuthorization,然后再调用registerForRemoteNotifications 不起作用。即使应用程序出现在带有Badges, Sounds, Banners 选项的 macOS 通知首选项窗格中,通知也不会显示。只有当我使用registerForRemoteNotificationTypes 方法时它才有效。
【解决方案2】:

澄清一下,这是 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])
}

【讨论】:

  • NSApp 是 NSapplication.shared: developer.apple.com/documentation/appkit/nsapp
  • 和 NSApp.registerForRemoteNotifications() 的文档在这里:developer.apple.com/documentation/appkit/nsapplication/…
  • 您通常应该提供工作代码。只是说NSApp 不会编译。 NSApp.registerForRemoteNotifications(matching: [.alert, .badge, .sound]) 甚至不是 macOS 中的方法。 registerForRemoteNotifications 不接受 macOS 中的任何参数。你看过我的回答吗?我的回答有问题吗?根据我向 Apple 提交的错误,这已在 macOS 10.14.4 中修复。我还没有时间升级和测试它。
  • NSApp 会编译。它从 10.0 开始在 macOS 中:“macOS 10.0+”registerForRemoteNotifications(matching: [.alert, .badge, .sound])developer.apple.com/documentation/appkit/nsapplication/…,它从 macOS 10.7 开始可用,并在 10.14 Mojave 中被弃用 - 请在评论之前查看 Mac 文档。
  • 很好。你仍然没有回答我的任何一个问题。而且我不确定您为什么建议使用已弃用的方法...
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多