当收到推送通知时,应用程序可以根据用户词典决定显示警报吗?

发布于 2024-12-05 04:06:13 字数 453 浏览 1 评论 0原文

当您收到推送通知时,是否可以让您的应用程序决定是否显示 AlertView 来指示您收到了推送通知?

或者操作系统只是自动控制这一点而不让您对此有任何控制权?

我希望收到一些发送给我的应用程序的通知,然后让我的应用程序根据 didReceiveRemoteNotification,是否显示警报或某些用户可见的通知?这有可能以任何方式吗?

Is it possible when you receive a push notification to somehow have your application decide whether or not to show the AlertView indicating you received a push notification?

Or does the OS just automatically control this and not let you have any control in this?

I would like to receive some notification to my app and then let my app decide based on some data in the dictionary that is passed in with didReceiveRemoteNotification, whether or not to show the Alert or some user visible notification? Is this possible in ANY way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷雾森÷林ヴ 2024-12-12 04:06:13

您无法控制通过应用程序推送的 UIAlertView 的外观。但服务器可能可以:尝试发送不带警报信息的推送通知。

请告诉我它是如何工作的。

You cannot control appearance of UIAlertView about Push through your application. But may be server could: try to send Push-notification w/o alert information.

Tell me, please, how it works.
TY

偏爱自由 2024-12-12 04:06:13

距离这个问题提出已有10年了,还有17天。现在,从 iOS 10.0 开始,您就可以了。

It's 17 days until it's 10 years since this question was asked. Now, you can, since iOS 10.0`. ????

I just wrote an answer to how to get the userInfo in other delegate methods. In your specific case, you want to implement the userNotificationCenter(_:willPresent:withCompletionHandler:).

Get the userInfo dictionary in that delegate method by using notification.request.content.userInfo. If you set a key/value such as foregroundNotification: true in the data payload passed to APNs, your device can see that here.

All you have to do is:

let userInfo = notification.request.content.userInfo
if let foregroundNotification = userInfo["foregroundNotification"] as? Bool, foregroundNotification == true {
    completionHandler([.banner])
}

More advanced handling (sound, supporting iOS 10+, including deprecations)

Bear in mind alert has been deprecated in iOS 14, and you may want to add iOS version checks, sound, list.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    var options: UNNotificationPresentationOptions = []
    let userInfo = notification.request.content.userInfo
    
    if let foregroundSound = userInfo["foregroundSound"] as? Bool, foregroundSound == true {
        options.insert(.sound)
    }
    
    if let foregroundBadge = userInfo["foregroundBadge"] as? Bool, foregroundBadge == true {
        options.insert(.badge)
    }
    
    if let foregroundList = userInfo["foregroundList"] as? Bool, foregroundList == true {
        if #available(iOS 14.0, *) {
            options.insert(.list)
        }
    }
    
    if let foregroundNotification = userInfo["foregroundNotification"] as? Bool, foregroundNotification == true {
        if #available(iOS 14.0, *) {
            options.insert(.banner)
        } else {
            options.insert(.alert)
        }
    }
    
    completionHandler(options)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文