I'm building an iOS app with FCM push notifications. I followed the Google Firebase docs and other tutorials, but my AppDelegate never execute the function didReceiveRemoteNotification, so when I send notification from my Firebase Console it does nothing.
I have this, for now:
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.registerForRemoteNotifications()
FIRApp.configure()
connectToFcm()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("<-TOKEN->")
print(token)
}
func connectToFcm() {
FIRMessaging.messaging().connect { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
}
- I also installed Firebase on my CocoaPods file.
- I registered the App in the Firebase Console with the same Build ID.
- I have "Required Background Modes -> App downloads content in response to push notifications" in my info.plist file
- I've created and uploaded my APNS Certificate to my settings in Firebase Console.
And when I start the App, it builds succesfully, and prints my this on console:
[Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
2017-05-29 12:10:56.101 incidenciasSifu[1811] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
Optional(Error Domain=com.google.fcm Code=2001 "FIRMessaging is already connected" UserInfo={NSLocalizedFailureReason=FIRMessaging is already connected})
<-TOKEN->
Optional("03545fbbb986e2ffdfa50ac9e3eb6fe07e6fe4694cdfd000d673a0bf5ea53f6a")
Connected to FCM.
I'm newbie on Swift, and I don't know if I forgot to add some code in AppDelegate or something.
Help please! I spent too much hour on this :(