sohan vanani sohan vanani - 4 months ago 326
Objective-C Question

FIRInstanceID getting nil in FIRInstanceIDAPNSTokenType.Sandbox

Hello I am using first time FCM push notification I have implemented FCM with this code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
FIRApp.configure()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
}
func tokenRefreshNotification(notification: NSNotification) {
print("refresh token call")
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")

NSUserDefaults.standardUserDefaults().setObject(refreshedToken, forKey: "deviceToken");
// Connect to FCM since connection may have failed when attempted before having a token.

connectToFcm()
updateDeviceToken()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
/*when I comment above line app works fine but not receiving notificaiton
in background and If i uncomment above line than app crashes
for the first time when I launch on device */
}
func applicationDidBecomeActive(application: UIApplication) {
connectToFcm()
}


Console log is printing
<FIRInstanceID/WARNING> APNS Environment in profile: development
before app crash.

Now issue is when I set
FIRInstanceIDAPNSTokenType.Sandbox
then app crashes when i launch first time bcaz of i am getting
FIRInstanceID.instanceID().token()!
nil in
tokenRefreshNotification
method.
And if I not set any APNS Token to
FIRInstanceID
then app works fine but i am not receiving background notification in this scenario. and also give me solution how to check
FIRInstanceID.instanceID().token()!
is nil or not

Answer

I just solved my problem with crash handling into tokenRefreshNotification Method. I am checking TokenId is nil or not with this code and now my app is not crashing and also I am getting token id:

func tokenRefreshNotification(notification: NSNotification) {
      //  print("refresh token call")
        guard let contents = FIRInstanceID.instanceID().token()
        else {
            return
        }
           // let refreshedToken = FIRInstanceID.instanceID().token()!
            print("InstanceID token: \(contents)")

            NSUserDefaults.standardUserDefaults().setObject(contents, forKey: "deviceToken");
            // Connect to FCM since connection may have failed when attempted before having a token.

            connectToFcm()
            updateDeviceToken()
    }