Show Beacon Notification - xamoom/xamoom-ios-sdk GitHub Wiki

When successfully completed the iBeacon setup, you can now display a beacon notification. This notifications will be shown in fore and background when a user is nearby a beacon.

1. NotificationCenter observer

To show a Notification for a ranged Beacon, you should implement the NotificationCenter observer for BEACON_RANGE in your AppDelegate. Every time a new Beacon is ranged this Observer will be triggered and a new Notification can be send.

NotificationCenter.default.addObserver(self, selector: #selector(didRangeBeacons(notification:)),
                   name: NSNotification.Name(rawValue: BEACON_RANGE),
                   object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didExitRegion(notification:)),
                   name: NSNotification.Name(rawValue: BEACON_EXIT),
                   object: nil)

2. Show a notification

In the Observers selector function you receive a Notification object. To get all loaded content items check the notifications userInfo dictionary for XAMOOM_CONTENTS_KEY. Now put the first element in this Array and show the content related information as LocalNotification.

@objc func didRangeBeacons(notification: Notification) {
   var content : XMMContent? = nil
   if let beacons = notification.userInfo?[XAMOOM_CONTENTS_KEY] as? [XMMContent], let content = beacons.first {
      let notification = UILocalNotification()
      notification.alertTitle = content.title
      notification.alertBody = content.description
      notification.fireDate = Date()

      UIApplication.shared.scheduleLocalNotification(notification)
   }
}

3. Remove Notifications

To remove all old Notifications and the badge, use the selector function of the BEACON_EXIT Observer.

@objc func didExitBeaconRegion(notification: Notification) {
   if #available(iOS 10.0, *) {
      let center = UNUserNotificationCenter.current()
      center.removeAllPendingNotificationRequests()
      center.removeAllDeliveredNotifications()
   } else {
      UIApplication.shared.cancelAllLocalNotifications()
   }
   UIApplication.shared.applicationIconBadgeNumber = 0
   UIApplication.shared.cancelAllLocalNotifications()
}

Next Step