首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册iOS 10+通知-未收到通知

注册iOS 10+通知-未收到通知
EN

Stack Overflow用户
提问于 2018-03-22 00:15:15
回答 1查看 162关注 0票数 0

在iOS 10之前,我可以注册并接收通知。现在我什么也没收到。下面是我的代码:

代码语言:javascript
复制
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
            if error == nil {

                if granted {
                    print("granted")

                    DispatchQueue.main.async {

                        UIApplication.shared.registerForRemoteNotifications()
                    }
                } else {
                    print("not granted")
                }
            } else {

            }
        }
    } else {
        // Fallback on earlier versions

        let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [UIUserNotificationType.badge, UIUserNotificationType.sound, UIUserNotificationType.alert], categories: nil)

        UIApplication.shared.registerForRemoteNotifications()
        UIApplication.shared.registerUserNotificationSettings(settings)
    }

顺便说一下,granted有时是真的,有时是假的。无论哪种方式,我都没有收到通知。回调函数与通知工作时的回调函数相同。

EN

回答 1

Stack Overflow用户

发布于 2018-03-22 00:33:52

您的代码看起来是正确的,但您可以尝试如下所示。

代码语言:javascript
复制
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) {[weak self] (granted, error) in

      guard error == nil else { return }

      if granted {

        // register for remote notifications
        UIApplication.shared.registerForRemoteNotifications()

        // set delegate
        UNUserNotificationCenter.current().delegate = self

      }
 }

您将通过以下方式收到ios 10的通知。

代码语言:javascript
复制
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    completionHandler([.alert, .sound, .badge])
}

当您点击通知时,将调用以下方法。

代码语言:javascript
复制
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49411461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档