Initialize FCM in Swift

suggest change

follow below step to add FCM in your swift Project

1- If you don’t have an Xcode project yet, create one now. Create a Podfile if you don’t have one:

$ cd your-project directory

$ pod init

2- Add the pods that you want to install. You can include a Pod in your Podfile like this:

pod ‘Firebase/Core’

pod ‘Firebase/Messaging’

3- Install the pods and open the .xcworkspace file to see the project in Xcode.

$ pod install

$ open your-project.xcworkspace

4- Download a GoogleService-Info.plist file from plist and include it in your app.

5- Upload APNs certificate to Firebase. APN Cert

6- add “import Firebase” in your appDelegate file of project

7- add this “FIRApp.configure()” in your “application:didFinishLaunchingWithOptions”

8- register for remote notification

if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
  authOptions,
  completionHandler: {_,_ in })

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self

} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

9- to get register token use

let token = FIRInstanceID.instanceID().token()!

10- and if u want monitor for token change use below code in appDelegate file

func tokenRefreshNotification(notification: NSNotification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
    print("InstanceID token: \(refreshedToken)")
  }

  // Connect to FCM since connection may have failed when attempted before having a token.
  connectToFcm()
}

11- to recieve message from fcm add below code in appDelegate

func connectToFcm() {
  FIRMessaging.messaging().connectWithCompletion { (error) in
    if (error != nil) {
      print("Unable to connect with FCM. \(error)")
    } else {
      print("Connected to FCM.")
    }
  }
}

12- and for disconnect use

func applicationDidEnterBackground(application: UIApplication) {
  FIRMessaging.messaging().disconnect()
  print("Disconnected from FCM.")
}

in your appDelegate.

the initialization complete and client ready to recieve message from fcm panel or send by token from third party server

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


FCM Messaging:
* Initialize FCM in Swift

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
138 FCM Messaging
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable