iOS
First to start iOS integration, be sure that Setup – Firebase setup was fully completed.
-
Enable a Push Notifications capability in App > Signing & Capabilities > + Capability > Push Notifications. Also be sure you provided a Build Identifier according to the your provisioning profile (check Apple Developer Programs if you don't have one).
Note
Push Notifications are available from iOS 10 or macOS 10.12 and later.
-
Put
GoogleService-Info.plist
right into the root of the directory as below: -
Add CocoaPods to the project:
- Initialize:
pod init
-
Add to
Podfile
:pod 'Firebase/Analytics' pod 'Firebase/Messaging'
pod 'Firebase/Messaging'
-
Install dependencies:
pod install
- Initialize:
-
Integrate the Firebase SDK.
-
Import the Firebase in your
UIApplicationDelegate
(AppDelegate
):import Firebase
@import Firebase;
-
Configure the Firebase instance in the application method with parameters
application:didFinishLaunchingWithOptions:
, basically, it isfunc application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
with the method:Firebase.configure()
[FIRApp configure];
-
Setup the application to receive the remote notifications using
UNUserNotificationCenter
:func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { FirebaseApp.configure() Messaging.messaging().delegate = self UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: { _, _ in } ) application.registerForRemoteNotifications() return true }
-
Setup the application to receive device tokens. Insert the code in the
AppDelegate
:func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { // set option to monitor token regeneration let dataDict: [String: String] = ["token": fcmToken] NotificationCenter.default.post( name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict ) // send request to the NeuCurrent method for token registartion according to NeuCurrent API Swagger UI }
Request sending with NeuCurrent API
Please, notice that in the code you should send a HTTP request to NeuCurrent API with a received device token on
https://app.neucurrent.com/push/api/v1/device-token/add
. Docs:https://app.neucurrent.com/push/api/v1/docs
-
-
Add ability to receive an image:
- Add a new target: File > New > Target > Notification Service Extension
- Add to
Podfile
:target 'YourNotificationServiceExtensionTargetName' do pod 'Firebase/Messaging' end
- Change
NotificationService.swift
file as below:NotificationService.swift1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import UserNotifications import FirebaseMessaging class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent guard let bestAttemptContent = bestAttemptContent else { return } FIRMessagingExtensionHelper().populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler) } override func serviceExtensionTimeWillExpire() { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } }
-
Congratulations! You've finished the integration process. Full example:
AppDelegate.swift1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import UIKit import Firebase @available(iOS 10.0, *) @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate { func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { FirebaseApp.configure() Messaging.messaging().delegate = self UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: { _, _ in } ) application.registerForRemoteNotifications() return true } func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { // set option to monitor token regeneration let dataDict: [String: String] = ["token": fcmToken] NotificationCenter.default.post( name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict ) // send request to the NeuCurrent method for token registartion according to Swagger UI } }
Additional sources: