Application most important feature is push notification but it’s tough to implement using APNS as compared to Firebase. These steps you have to follow using push notification in iOS.
- Go to https://developer.apple.com/ site choose account then log in After Login follows this link to generate a certificate.p12 file https://support.magplus.com/hc/en-us/articles/203808748-iOS-Creating-a-Distribution-Certificate-and-p12-File
- Then open https://console.firebase.google.com/
- Create a new project.
- After creating a project you have to choose the add app option click on it provide your application bundle identifier and your application name
- download the GoogleService-Info.plist and Firebase SDK.
- Click on the gear button on the left top -> project settings -> cloud messaging then upload your certificate in (production APNs certificate and development APNs certificate).
- Go to your project in Xcode.
- The step you have to follow configure firebase in your application and download firebase from this link https://github.com/firebase/firebase-ios-sdk and the following frameworks into your app.
- FirebaseMessaging.framework
- module.modulemap
- FirebaseAnalytics.framework
- FirebaseCore.framework
- FirebaseInstanceID.framework
- GoogleInterchangeUtilities.framework
- GoogleSymbolUtilities.framework
- GoogleToolboxForMac.framework
- libsqlite3.tbd
- AdSupport.framework
and add these flags go to Build Settings – choose Other Linker Flags: – add the following flags -lstdc++ -ObjC -lz
- Click on your project target and go to Capabilities. In Capabilities on push notification and Background Modes, in Background Modes enable Background fetch and Remote Notifications.
- All your firebase is setup now go to the AppDelegate.swift class in your project add the following code:-
1 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) FIRApp.configure() NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil) return true } // get the device token func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { var tokenq = "" for i in 0..<deviceToken.count { tokenq = tokenq + String(format: "%02.2hhx", arguments: [deviceToken[i]]) } print(tokenq) FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.sandbox ) FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.prod ) } // get instance firebase token func tokenRefreshNotification(_ notification: Notification) { 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() } func connectToFcm() { FIRMessaging.messaging().connect { (error) in if error != nil { print("Unable to connect with FCM. \(error)") } else { print("Connected to FCM.") } } } // get you message func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { print("Message ID: \(userInfo["gcm.message_id"]!)") // Print full message. print(userInfo) } |
6. Then you can send messages from Firebase console you can notification from there.
7. If you want to send notification from postman or implement from server side follow this link https://mobikul.com/send-push-notification-via-firebase-ios-postman/