2017-03-18 16 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の10のためにあなたがテストや開発が、 されているバージョンのIOSていることを確認していません取得/取得デバイストークンのセットアップ。 Firebaseインテグレーションの文書に従ってください。https://firebase.google.com/docs/cloud-messaging/ios/client

必要なリンクをクリックしてください。ここで

私はトークンデバイスを取得するために

import UserNotifications //For iOS 10 

登録ためUNUserNotificationCenterDelegate

// Appdelegate方法

輸入UserNotificationsフレームワーク、いくつかの重要な事柄を列挙しています

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文字列のいずれかをログインコードに呼び出そうとすると、これも動作しません。それを空にし続ける! –

関連する問題