2015-10-08 39 views
5

Tôi đang sử dụng này để đăng ký thông báo:Đăng ký thông báo trong iOS 9

if application.respondsToSelector("registerUserNotificationSettings:") { 
       let types:UIUserNotificationType = (.Alert | .Badge | .Sound) 
       let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil) 

       application.registerUserNotificationSettings(settings) 
       application.registerForRemoteNotifications() 

      } else { 
       // Register for Push Notifications before iOS 8 
       application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound) 
      } 

Nhưng nó không Woking giải pháp dành cho iOS 9.

Trả lời

10

tôi đã tìm thấy:

if application.respondsToSelector("registerUserNotificationSettings:") { 
     if #available(iOS 8.0, *) { 
      let types:UIUserNotificationType = ([.Alert, .Sound, .Badge]) 
      let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
     } else { 
      application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge]) 
     } 
    } 
    else { 
     // Register for Push Notifications before iOS 8 
     application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge]) 
    } 

Đoạn mã trên đang làm việc hoàn hảo cho tôi.

Các vấn đề liên quan