在应用程序安装iOS swift的第一次获取firebase令牌nill?

时间:2017-11-20 04:12:20

标签: ios firebase swift3 apple-push-notifications firebase-cloud-messaging

import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging



@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

    var window: UIWindow?
    var plistHelper = PlistHelper()
    var authenticateService = AuthenticateService()
    var userService = UserService()
    var utilityHelper = UtilityHelper()
    var alertHelper = AlertHelper()
    var notificationHelper = NotificationManager()
    var preferenceService = PreferenceService()
    var utilityService = UtilityService()


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


        //For FireBase Configs
        Messaging.messaging().delegate = self

        let settings: UIUserNotificationSettings =
           UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

        FirebaseApp.configure()
        connectToFcm()

        NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification(_:)), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)

        //FireBase Configs



        return true
    }



    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        NSLog("show notification")
        completionHandler(UNNotificationPresentationOptions.alert)
    }


    func application(received remoteMessage: MessagingRemoteMessage) {


        print(" APNSData \(remoteMessage.appData)")
        print(" APNSData Count\(remoteMessage.appData.count)")
        print(" APNSData NotificationType:-\(remoteMessage.appData.index(forKey: "NotificationType")!)")
    }
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        NSLog("Refreshed Firebase registration token: \(fcmToken)")

        let appVersion = plistHelper.getAnyValueFromPlistFile("APP_VERSION")
        let firebaseDeviceToken = plistHelper.getAnyValueFromPlistFile("FIREBASE_DEVICE_TOKEN")
        let dateFormatter1 = DateFormatter()
        dateFormatter1.dateStyle = .medium
        dateFormatter1.timeStyle = .none
        dateFormatter1.dateFormat = "yyyy-MM-dd"
        let today = dateFormatter1.string(from: NSDate() as Date)


        //New token
        plistHelper.addValueToPlistFile(fcmToken, Key: "FIREBASE_DEVICE_TOKEN")

        if(firebaseDeviceToken != fcmToken){
            let userRegvm = UserDeviceRegistrationViewModel()
            userRegvm.UserDeviceRegistrationId = 1
            userRegvm.UserId = ""
            userRegvm.DeviceType = DeviceType.IOS.rawValue
            userRegvm.EmieNumber = ""
            userRegvm.Make = "Apple"
            userRegvm.Model = "Apple"
            userRegvm.UpdatedTime = today
            userRegvm.AppVersion =  appVersion
            userRegvm.DeviceId = fcmToken
            userRegvm.ExpiredDeviceId = firebaseDeviceToken

            let result = userService.registerDevice2(userDeviceRegistrationvm: userRegvm)

            if(result){
                 NSLog("Device refreshed & Registered")
            }else{
                NSLog("Device refreshed Register Falied ")
            }

        }

    }



    func tokenRefreshNotification(_ notification: Notification) {

        let token = InstanceID.instanceID().token()

    }

    func application(_ application: UIApplication,
                             didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken as Data


        NSLog("Register FireBase Token.")
        if let regToken = InstanceID.instanceID().token() {

            NSLog("FireBase Token: \(regToken)")
            //Save token
            plistHelper.addValueToPlistFile(regToken, Key: "FIREBASE_DEVICE_TOKEN")            

            let userRegvm = UserDeviceRegistrationViewModel()
            userRegvm.UserDeviceRegistrationId = 1
            userRegvm.UserId = ""           


            let result = userService.registerDevice2(userDeviceRegistrationvm: userRegvm)

            if(result){
                 NSLog("Device refreshed & Registered")
            }else{
                NSLog("Device refreshed Register Falied ")
            }           

        }
    }

}

我在我的应用中使用firebase进行推送通知。我使用的语言是swift 3.这是我的代码。 我面临的问题是我在安装应用时第一次没有收到firebase令牌。但安装后,如果我关闭应用程序并再次运行将返回一个firebase令牌。当应用程序安装的第一次请求推送通知的权限时,如果我允许它作为firebase令牌获取nil,则会出现此问题。我能知道这种方法出了什么问题吗?。

注意:我已经制作了这个" FirebaseAppDelegateProxyEnabled"在info.plist

中键入YES

任何帮助将不胜感激

0 个答案:

没有答案