2017-03-18 57 views
-1

我試圖發送IOS設備令牌數據庫使用它在通知使用PHP和Firebase,但始終獲取設備令牌空!iOS設備令牌發送到數據庫空

的AppDelegate代碼:

import UIKit 
import SlideMenuControllerSwift 
import GoogleMaps 
import Firebase 
import FirebaseCore 
import UserNotifications 
import Localize_Swift 

var appDefaults = UserDefaults.standard; 
var appDelegate = UIApplication.shared.delegate as! AppDelegate 
@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    var notificationtoken:String = ""; 

func application(application: UIApplication, 
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     FIRInstanceID.instanceID().setAPNSToken(deviceToken , type: FIRInstanceIDAPNSTokenType.prod) 

     self.notificationtoken = deviceToken.base64EncodedString(); 

     sendDeveiceToken(); 
    } 

} 

登入碼

import UIKit 

class LoginViewController: UIViewController ,UITextFieldDelegate{ 
    @IBOutlet weak var usernameTextField: UITextField! 

    @IBOutlet weak var passwordTextField: UITextField! 


    @IBOutlet weak var loginBtn: UIButton! 
var userlogin:userLogin? 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     usernameTextField.delegate = self; 
     passwordTextField.delegate = self; 
     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func LoginAction(_ sender: UIButton) { 

     Shared.HUD.progressHUD(nil, message: nil); 
     if (usernameTextField.text != nil && passwordTextField.text != nil) { 
      //userLogin(user_name: String , password :String, device: String , Token: String) 
      GoldenTajProvider.request(.userLogin(user_name: usernameTextField.text!, password: passwordTextField.text!,Token: appDelegate.notificationtoken), completion: { (result) in 
       var success = true 
       var message = "Unable to fetch from GitHub" 
       Shared.HUD.hide(2) 
       switch result { 
       case let .success(moyaResponse): 
        do { 
         let repos = try moyaResponse.mapObject(userLogin) 
         if (repos.data != nil){ 
         self.userlogin = repos; 
         if let user = self.userlogin{ 
          userLogin.updateUserObject(user) 

          appDelegate.currentUserId = (user.data?.first?.iD!)! 
          appDelegate.currentUser = user; 
         } 

         UserDefaults.standard.setValue(accessToken, forKey: AppUserDefaults.AccessToken.rawValue); 
         UserDefaults.standard.set(true, forKey: AppUserDefaults.isLogedIn.rawValue); 
         appDelegate.presentMain(); 
         }else { 
          let json = try moyaResponse.mapJSON() as! [String:Any] 
          message = json["data"] as! String 
          Shared.Alert("", message: message, confirmTitle: "OK", sender: self); 


         } 
        } catch { 
         success = false 
        } 

       case let .failure(error): 
        guard let error = error as? CustomStringConvertible else { 
         break 
        } 
        message = error.description 
        success = false 
       } 

      }) 

     } 



    } 


    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     self.view.endEditing(true) 
     return false 
    } 
} 

回答

0

我不知道在IOS版本正在測試或開發,但 爲iOS 10,你需要做更多的設置檢索/獲取設備令牌。 您應該遵循Firebase集成文檔https://firebase.google.com/docs/cloud-messaging/ios/client

請按照您所需的鏈接進行操作。

我在這裏列出一些重要的事情

進口UserNotifications框架

import UserNotifications //For iOS 10 

註冊UNUserNotificationCenterDelegate

//的appdelegate方法來獲取設備令牌

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Prod) 
    } 

func registerForPushNotifications(application: UIApplication) { 

    if #available(iOS 10.0, *){ 
     UNUserNotificationCenter.currentNotificationCenter().delegate = self 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in 
      if (granted){ 
       UIApplication.sharedApplication().registerForRemoteNotifications() 
      } 
     }) 
    }else{ //If user is not on iOS 10 use the old methods we've been using 
     let notificationSettings = UIUserNotificationSettings(
      forTypes: [.Badge, .Sound, .Alert], categories: nil) 
     application.registerUserNotificationSettings(notificationSettings) 
    } 
} 

注意:這些方法完全記錄在Firebase文檔中。

+0

即使有這個,當我嘗試發送設備令牌到web服務它變空:var notificationtoken:String =「」; self.notificationtoken = deviceToken.base64EncodedString(); –

+0

使用let deviceToken獲取設備令牌:String? = FIRInstanceID.instanceID()。token() –

+0

只要嘗試將任何appdelegate字符串調用爲登錄代碼,這也不起作用。保持空着! –