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

查看:26
本文介绍了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天全站免登陆