macOS远程推送通知未显示警报横幅 [英] macOS Remote Push Notifications Not Showing Alert Banner

查看:94
本文介绍了macOS远程推送通知未显示警报横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的macOS应用程序启用推送通知.一切似乎都正常.我可以获取设备令牌.发送通知没有错误.除非我的Mac上没有显示任何警报.

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.

我已经在iOS上测试了完全相同的设置,并且运行正常并在其中显示警告.因此,我必须特别在macOS上缺少某些内容.

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:

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

  1. 如果我用代码手动创建本地通知,则它可以正常工作,并且显示通知横幅
  2. 我无法在旧版本的macOS上进行测试,因为我使用的推送通知API刚刚在macOS Mojave中发布了
  3. 我也试图创建另一个测试项目,并且发生了相同的问题
  4. 我已经确保请勿打扰"功能已关闭,并且还在通知中心检查了该通知,但是该通知中心也未显示.

如何在MacOS上显示横幅并播放声音?

How can I get it to show the banner and play the sound on macOS?

推荐答案

在iOS中,以下代码可以正常工作:

In iOS the following code works fine:

UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
    guard granted else { return }

    DispatchQueue.main.async {
        application.registerForRemoteNotifications()
    }
}

对于macOS,我将该代码更改为:

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()
    }
}

找出 NSApplication.shared.registerForRemoteNotifications()行是错误的.在macOS上,您必须传递与此呼叫相同的选项.

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])

我发现奇怪的是,在 Apple的文档中说该方法已被弃用,我们应该改用 registerForRemoteNotifications().这使我认为 registerForRemoteNotifications()存在某种类型的错误,导致通知无法正确显示.

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.

修改

Apple已通知我,此问题已在macOS 10.14.4中修复.我还无法升级到最佳状态并进行测试.因此,我目前无法确认.有机会在macOS 10.14.4上进行测试时,我将对此进行更新.

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.

这篇关于macOS远程推送通知未显示警报横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆