2016-12-26 101 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. 
    } 

而且也确保你的服务器端脚本运行良好......