Push Notifications - xamoom/xamoom-ios-sdk GitHub Wiki
In this topic you will learn how to handle Push Notification, register a device for GeoPush and how to handle GeoPush.
With our push notification service you can either send a notification to every user, or just a selected group of users who are in a certain radius around a point. To handle them in your app, you have to implement our PushHelper class. This class handles the FCM token registration and the received message for you. You just have to implement the Google-Service.plist file like described here and register your app for APN.
Setup
First of all you have to create your PushHelper instance in AppDelegates didFinishLaunchingWithOptions.
let helper = PushHelper.init(api: ApiHelper.shared.api)
The PushHelper class handles the FirebaseMessagingDelegate and the UNUserNotificationCenterDelegate for you. To use these delegates set them in your AppDelegate like this.
Messaging.messaging().delegate = helper?.messagingDelegate
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
let center = UNUserNotificationCenter.current()
center.delegate = helper?.notificationDelegate
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
Notification interaction
The PushHelper class managing the FCM delegate functions and the UNUserNotificationCenterDeleagte functions for you. To trigger a Notification tap, you have to implement the NotificationCenter observer for XAMOOM_NOTIFICATION_RECEIVE in your AppDelegate.
NotificationCenter.default.addObserver(self, selector: #selector(openContent(notification:)),
name: NSNotification.Name.init(XAMOOM_NOTIFICATION_RECEIVE),
object: nil)
@objc func openContent(notification: NSNotification) {
if let contentId = notification.userInfo?["content-id"] as? String {
let controller = ContentViewController(nibName: "ContentViewController", bundle: Bundle.main)
controller.contentId = contentId
controller.isBeacon = false
controller.hidesBottomBarWhenPushed = true
self.navigationController.pushViewController(controller, animated: true)
}
}
The Observer post to the NotificationCenter when the userNotificationCenter:(UNUserNotificationCenter )center didReceiveNotificationResponse:(UNNotificationResponse )response withCompletionHandler:(void (^)(void))completionHandler function in PushHelper triggers.
To react to the Observer add the selector function like this.
PushDevice
For using Push you have to store your FCM token in our XMMSimpleStorage, to store the token in the UserDefaults. This is required, because the pushDevice function of XMMEnduserApi class will load the token and the last user location from XMMSimpleStorage. If one of them is nil, the call will not be send to the backend and the device will not be registered for Push. Every time the FCM token is refreshing, the PushHelper class updates the FCM token, but at one part of the code you have to do this for your own.
In AppDelegate's application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) function, you have to save the FCM token like this.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
InstanceID.instanceID().instanceID { (result, error) in
if let result = result {
print("Remote instance ID token: \(result.token)")
print("token: ", result.token)
XMMSimpleStorage().saveUserToken(result.token)
api.pushDevice()
} else if let error = error {
print("Error fetching remote instance ID: \(error)")
}
}
}
After receiving a new token you have to call pushDevice function of XMMEnduserApi class for updating the users device in the system.
User location
The LocationHelper class of our SDK handles the user location change for you. Here you can find how to use this class.