Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift macOS远程推送通知未显示警报横幅_Swift_Macos_Push Notification_Apple Push Notifications - Fatal编程技术网

Swift macOS远程推送通知未显示警报横幅

Swift macOS远程推送通知未显示警报横幅,swift,macos,push-notification,apple-push-notifications,Swift,Macos,Push Notification,Apple Push Notifications,我正在尝试为我的macOS应用程序启用推送通知。一切似乎都在运转。我能拿到设备令牌。发送通知时没有错误。除了我的Mac电脑上没有显示警报 我添加了以下代码,以查看我的应用程序是否收到了它 func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) { print(userInfo) } 发送通知后,我在控制台中看到以下内容 ["aps": {

我正在尝试为我的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.registerforremotentifications()
    不正确。在macOS上,您必须将提供的相同选项传入此通话

    将该行更改为以下行是有效的

    NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])
    
    我觉得奇怪的是,其中说该方法已被弃用,我们应该使用
    registerforremotentifications()
    。这让我觉得
    registerForRemoteNotifications()
    中存在某种类型的错误,通知显示不正确

    还有一件事要提。确实需要一点时间(几分钟),并发送一些通知,以便它们在做出更改后实际出现。不确定这是否只是由于互联网连接速度慢或什么原因。但是现在它们在被发送后很快就出现了


    编辑


    苹果告诉我,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])
    }
    

    >这让我觉得registerForRemoteNotifications()存在某种类型的错误,其中通知显示不正确。我agree@erkanyildiz您是否遇到相同的问题?是的。在
    unuservificationcenter
    上调用
    requestAuthorization
    ,然后调用
    registerforremotentifications
    不起作用。即使应用程序显示在macOS Notifications pref窗格中,带有
    徽章、声音、横幅
    选项,也不会显示通知。只有当我使用
    registerforremotentifications类型
    方法时,它才起作用。NSApp是NSapplication.shared:并且NSApp.registerforremotentifications()的文档在这里:您通常应该提供工作代码。仅仅说
    NSApp
    不会编译。另外,
    NSApp.registerforremotentifications(匹配:[.alert、.badge、.sound])
    甚至不是macOS中的一种方法
    registerforremotentifications
    不接受macOS中的任何参数。你看过我的答案了吗?我的回答有问题吗?根据我向苹果提交的bug,这在macOS 10.14.4中已经修复。我还没来得及升级和测试它。NSApp确实可以编译。它从10.0开始在macOS中出现:“macOS 10.0+”
    RegisterForremotentifications(匹配:[.alert、.badge、.sound])
    is,它从macOS 10.7开始出现,在10.14 Mojave中被弃用-请在评论之前查阅Mac文档。很好。你仍然没有回答我的任何一个问题。我不知道你为什么建议使用不推荐的方法……我知道你在回答中已经指定了这一点,但我没有看到我什么时候通过了,所以我认为这可能有助于一些人看到这张图片。]()