2016-12-26 92 views
-3

最初的工作,我們導入UserNotifications框架 ,我們在AppDelegate.swift類初始化委託方法推送通知不SWIFT 3.0

if #available(iOS 10.0, *){ 

     UNUserNotificationCenter.current().delegate = self 
     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
      if (granted) 
      { 
       UIApplication.shared.registerForRemoteNotifications() 
      } 
      else{ 
       //Do stuff if unsuccessful... 
      } 
      // Enable or disable features based on authorization. 
     } 

    } 
+0

你有沒有檢查http://stackoverflow.com/questions/41035878/how-可以-I-STORE-推通知警報消息功能於userdefault/41037919#41037919 – Amanpreet

回答

-1

請檢查開關下Target > Capabilities的「推送通知」:

打開它

-1

更改代碼這樣

let center = UNUserNotificationCenter.current() 
    center.delegate = self 
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
     if (granted) 
     { 
      UIApplication.shared.registerForRemoteNotifications() 
     } 
     else{ 
      //Do stuff if unsuccessful... 
     } 
     // Enable or disable features based on authorization. 
    } 

而且也確保你的服務器端腳本運行良好......