我正在尝试将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
}
}
答案 0 :(得分:0)
我不确定您正在测试或开发哪个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文档中。