UIUserNotificationSettings在iOS 10中已被弃用。

33

我正在寻找在iOS 10中实现UIUserNotificationSettings的替代方法。

苹果文档提供了以下框架供进一步使用。UNNotificationSettings链接在这里

是否有人可以帮助我使用UNNotificationSettings而不是UIUserNotificationSettings实现以下示例代码?

let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)

1
抱歉,我刚在这里找到了答案。https://dev59.com/lloT5IYBdhLWcg3w-TRV - Praveen Kumar
1个回答

71

您需要实现以下代码以注册通知:

import UserNotifications

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { 
 (granted, error) in
 //Parse errors and track state
}

这个资源包含更多信息。


6
请不要忘记在这段代码下方添加application.registerForRemoteNotifications() - Bhavin_m
@iOS.Wolf 这只有在你计划使用不仅仅是本地通知的推送通知时才是必要的。 - Gabriel Garrett
4
不要忘记链接UserNotifications.framework并且使用@import UserNotifications。 - NickDK
与UserNotifications.framework链接的好技巧!! - Fattie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接