【问题标题】:IOS Device token sent empty to databaseIOS 设备令牌空发送到数据库
【发布时间】:2017-03-18 01:30:12
【问题描述】:

我正在尝试将 IOS 设备令牌发送到数据库,以便在使用 php 和 Firebase 的通知中使用它,但始终让设备令牌为空!!

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
    }
}

【问题讨论】:

    标签: ios is-empty devicetoken


    【解决方案1】:

    我不确定您正在测试或开发哪个 ios 版本,但是 对于 iOS 10,您需要为检索/获取设备令牌进行额外设置。 您应该关注 https://firebase.google.com/docs/cloud-messaging/ios/client 上的 Firebase 集成文档

    点击你所需要的链接。

    这里我列出了一些重要的事情

    导入 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 文档中。

    【讨论】:

    • 即使这样,当我尝试将设备令牌发送到 Web 服务时,它也会变为空:var notificationtoken:String = ""; self.notificationtoken = deviceToken.base64EncodedString();
    • 使用 let deviceToken:String 获取设备令牌? = FIRInstanceID.instanceID().token()
    • 这在尝试将任何 appdelegate 字符串调用到登录代码时也不起作用。继续让它空着!
    • 希望你没有使用模拟器
    • 您必须在询问时允许推送通知,或者从设置中打开它
    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多