-jio-start-block-type-2
Follow below steps to Set up FCM Environment in your App for Jio Magnum SDK
-jio-style-title
Please refer to the link below to get you Application up and running with FCM.
https://firebase.google.com/docs/ios/setup
-jio-style-title
Step 1. Import JioPushFramework and Firebase in app delegate and create JPNPushHelper object.
let jpnPushHelper = JPNPushHelper()
Step 2. Initialize the Push framework and register for remote notification
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
JioPushSdk.shared.setAppEnvironment(environment: .preProduction)
//JioPushSdk.shared.setAppEnvironment(environment: .production)
JioPushSdk.shared.setLogLevel(logLevel: .DEBUG)
JioPushSdk.shared.initialize()
FirebaseApp.configure()
self.registerForFirebaseNotification(application: application)
Messaging.messaging().delegate = self
return true
}
func registerForFirebaseNotification(application: UIApplication) {
jpnPushHelper.jpnPushHelperDelegate = self
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
}
Step 4: Implement UserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.banner])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Handle the received notification
guard let cBody = response.notification.request.content.userInfo["c_body"] as? String else { return }
guard let jsoncBodyData = cBody.data(using: .utf8),
let jsoncBodyDict = try? JSONSerialization.jsonObject(with: jsoncBodyData, options: []) as? [String: Any],
let jsonPayloadDict = jsoncBodyDict["payload"] as? [String: Any] else {
//App side logic
return
}
if let ntType = jsonPayloadDict["ntType"] as? Int, ntType != 0 {
pushHelper.showNotification(notificationResponse: response)
} else {
// For S2S and Legacy notifications
// App side logic
var identifier = response.notification.request.identifier
if let jsonMessageMetaDict = jsoncBodyDict["messageMeta"] as? [String: Any],
let messageId = jsonMessageMetaDict["messageId"] as? String {
identifier = messageId
}
let notification = JPNNotification(notificationId: identifier, cBody: cBody, receivedTime: Int(getTimeInMilliSeconds()))
//For Clicked on notification
JioPushSdk.shared.onNotificationAction(actionType: .clicked, notification: notification)
// If Notification is Viewed
JioPushSdk.shared.onNotificationAction(actionType: .viewed, notification: notification)
}
}
}
Step 5: Register to the JioPushService
JPNRegistration.shared.deviceRegistration(fcmToken: token,
secondaryId: "AppSecondaryId",
appName: "AppName",
topicName: "TopicName")
Step 6: Register with Secondary ID
JPNRegistration.shared.deviceRegistration(fcmToken: token,
secondaryId: "AppSecondaryId",
appName: "AppName",
topicName: "")
Step 7: Register with Topic
JPNRegistration.shared.deviceRegistration(fcmToken: token,
secondaryId: "",
appName: "AppName",
topicName: "TopicName")
Note: We will provide the AppName
Step 8. Add the following Extension file (JioPushFramework+Extension.swift) to the app to support Subscribe and UnSubscribe to Topic based notification
import Foundation
import JioPushFramework
import Firebase
extension JPNRegistration {
func subscribeTo(topicName: String, appName: String) {
if !topicName.isEmpty && !appName.isEmpty {
let topic = "JP_\(appName.trimmed())_\(topicName.trimmed())-iOS"
Messaging.messaging().subscribe(toTopic: topic) { error in
if let _ = error {
print("Subscribe to \(topic) topic failed")
JPNRegistration
.shared
.delegate?
.onErrorResponse(errorCode: 404,
errorMessage: "Subscribing to \(topicName) Failed",
forRequest: .subscribe)
} else {
JPNRegistration
.shared
.subscribeToTopic(topicName: topicName,
appName: appName)
}
}
}
}
func unsubscribeTo(topicName: String, appName: String) {
if !topicName.isEmpty && !appName.isEmpty {
let topic = "JP_\(appName.trimmed())_\(topicName.trimmed())-iOS"
Messaging.messaging().unsubscribe(fromTopic: topic){ error in
if let _ = error {
JPNRegistration
.shared
.delegate?
.onErrorResponse(errorCode: 404,
errorMessage: "Unsubscribing to \(topicName) Failed",
forRequest: .unsubscribe)
} else {
print("Unsubscribe to \(topic) topic success")
JPNRegistration
.shared
.unsubscribeToTopic(topicName: topicName,
appName: appName)
}
}
}
}
}
extension String {
func trimmed() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
Step 9. Subscribe
JPNRegistration.shared.subscribeTo(topicName: topicName,
appName: appName)
Step 10. Unsubscribe
JPNRegistration.shared.unsubscribeTo(topicName: topicName,
appName: appName)
Delegates for Register, Subscribe, UnSubscribe and Tags request
extension AppDelegate: JPNResponseProtocol {
func onSuccessResponse(responseCode: Int,
responseMessage: String,
forRequest requestType: JioPushResponseType) {
switch requestType {
case .register:
print("Registration Successful")
break
case .subscribe:
print("Subscribe Successful")
break
case .unsubscribe:
print("UnSubscribe Successful")
break
case .tags:
print("Tag Creation Successful")
break
default:
break
}
}
func onErrorResponse(errorCode: Int,
errorMessage: String,
forRequest requestType: JioPushResponseType) {
switch requestType {
case .register:
print("Registration Failed")
break
case .subscribe:
print("Subscribe Failed")
break
case .unsubscribe:
print("UnSubscribe Failed")
break
case .tags:
print("Tag Creation Failed")
break
default:
break
}
}
}
extension AppDelegate: JPNPushHelperProtocol {
func onLoadURLAction(stringURL: String) {
// On load URL action
}
}
To get all the received notification:
let notificationList: [JPNNotification] = JioPushSdk.shared.getSavedNotifications()
-jio-end-block-type-2