AlertingService

public class AlertingService: NSObject

Use an entity of this class to get access to all SDKs key functionality.

An AlertingService object supports:

  • TWC Alerting service authentication
  • Creating, updating, deleting assets, asset registrations, viewing of available peril groups and perils
  • Tracking and updating user’s location automatically (follow me)

To configure and start using an AlertingService object:

  1. Instantiate new AlertingService object
  2. Call setup(clientId: clientSecret: launchOptions:) in your AppDelegate's application(_ application: didFinishLaunchingWithOptions:)
  3. Create an asset with create(_ asset) call
  4. Retrieve available peril groups with getAllGroups() call
  5. Prepare an AssetNotification object with one of two types (email or sms) and address (email address or phone number)
  6. Prepare an AssetRegistration object with your asset’s id, selected peril groups and prepared AssetNotification object
  7. Register an asset for selected groups with registerAsset(_ registration) call and pass prepared AssetRegistrationObject into it
  8. Call followMe() to start tracking user location automatically, or retrive the your last Asset object from getCurrentAsset() call, update its location and call update(_ asset) to update its location manually.

Note

Do not foget to stop tracking user location by calling setStaticMode() when it is no longer needed.
  • Notification name for asset location updated event.

    Declaration

    Swift

    public static let nAssetLocationUpdated = "assetLocationUpdated"
  • Notification name for asset location update failed event.

    Declaration

    Swift

    public static let nAssetLocationUpdateFailed = "assetLocationUpdateFailed"
  • Prepares AlertingService object for work.

    Declaration

    Swift

    public func setup(clientId: String, clientSecret: String, launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil)

    Parameters

    clientId

    IBM organisation client ID.

    clientSecret

    IBM organisation client Secret.

    launchOptions

    UIApllication’s launchOptions, can be obtained from AppDelegate’s application(_ application: didFinishLaunchingWithOptions:)

  • Constant indicating registration for Push Notifications on Quick Start

    Declaration

    Swift

    public static let kQuickStartAddressPushToken = "PUSH_NOTIFICATION_REGISTRSTION"
  • Fast way to sign up for notifications. It will create an Asset with randomly generated ID and register it for notifications.

    Note

    Location Always Usage permission required for enabling follow me.

    Declaration

    Swift

    public func quickStart(with address: String,
                               location: Location? = nil,
                               followMe: Bool = true,
                               perilGroups: [PerilGroup]? = nil,
                               successHandler: @escaping () -> Void = { _ in},
                               failureHandler mainFailureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    address

    Address for notification delivery (Phone number, email address). Set AlertingService.kQuickStartAddressPushToken if you are registering for APNs notifications.

    location

    Starting location, required for an Asset creation. If nil, it will try to get current device location.

    followMe

    Enables follow me on “true”, otherwise sets Static Mode. “true” by default.

    perilGroups

    List of peril groups that you want to subscribe to. If nil all available groups will be subscribed to. nil by default.

  • Saves APNs token for future registration.

    Call this on your AppDelegate’s application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) and pass token to it

    Declaration

    Swift

    public func registerForRemoteNotification(token: String)
  • Indicates whether Alert Service has APNs token.

    Declaration

    Swift

    public func gotAPNToken() -> Bool
  • Indicates current AlertingService object’s tracking work mode (.staticMode or .followMeMode)

    Declaration

    Swift

    public fileprivate(set) var currentWorkMode: AlertingServiceWorkMode?
  • Instantiates new AlertingService object.

    Declaration

    Swift

    override public init()
  • Instantiates new AlertingService object

    Declaration

    Swift

    public init(hostname: String? = nil)

    Parameters

    hostname

    Host name for Alerting Api requests. Pass nil to use default.

  • Sets currentWorkMode to .followMeMode. Turns on automatic user tracking.

    Note

    In this mode, app registers for significant location change event and will update current asset’s location every time that event fires, even when app is in background or suspended.

    Note

    Location Always Usage permission required.

    Declaration

    Swift

    public func followMe()
  • Sets currentWorkMode to .staticMode. Turns off automatic user tracking.

    Declaration

    Swift

    public func setStaticMode()
  • Creates new Asset. Sets created asset as current if request succeeds.

    Declaration

    Swift

    public func create(_ asset: Asset,
                           successHandler: @escaping (Asset) -> Void = { _ in},
                           failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    asset

    prepared Asset object with id and location fields filled in.

    successHandler

    closure that returns new created Asset when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Indicates if there is a current Asset object created.

    Declaration

    Swift

    public func assetIsCreated() -> Bool
  • Updates given Asset object.

    Declaration

    Swift

    public func update(_ asset: Asset,
                           successHandler: @escaping (Asset) -> Void = { _ in},
                           failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    asset

    Asset object that you would like to update.

    successHandler

    closure that returns updated Asset when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Deletes current Asset.

    Declaration

    Swift

    public func deleteCurrentAsset(successHandler: @escaping (Bool) -> Void = { _ in},
                                       failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    successHandler

    closure that returns Bool that represents deletion success when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Retrieves current Asset.

    Declaration

    Swift

    public func getCurrentAsset() -> Asset?
  • Gets an array of all available PerilGroup‘s for your organisation.

    Note

    Your organisation is determined by your client ID and Secret that you passed into setup(clientId: clientSecret: launchOptions:) in your AppDelegate's application(_ application: didFinishLaunchingWithOptions:) call.

    Declaration

    Swift

    public func getAllGroups(successHandler: @escaping ([PerilGroup]) -> Void = { _ in},
                                 failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    successHandler

    closure that returns an array of PerilGroups when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Registers current Asset for a given PerilGroup‘s and AssetNotification in AssetRegistration object that you pass as a parameter.

    Note

    In case if SDK has not APNs token and user tries to register for remote notification, failureHandler closure will be called immediately with AlertingApiError returned in it, you can check AlertingApiEror’s code value to be RegistrationErrorCode.sdkHasNoTokenForPushRegistration to see if your request failed due to token absence.

    Make sure that you call registerForRemoteNotification(token:) on your AppDelegate’s application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

    Declaration

    Swift

    public func registerAsset(_ registration: AssetRegistration,
                                  successHandler: @escaping (AssetRegistration) -> Void = { _ in},
                                  failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    registration

    prepared AssetRegistration object with notification and groups values set.

    successHandler

    closure that returns new created AssetRegistration when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Gets an array of all registrations for the current Asset.

    Declaration

    Swift

    public func getAllAssetRegistrations(successHandler: @escaping ([AssetRegistration]) -> Void = { _ in},
                                             failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    successHandler

    closure that returns an array of AssetRegistration’s when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Updates given AssetRegistration object.

    Note

    In order to change delivery address this method deletes previous registretion and creates new one.

    -note: In case if SDK has not APNs token and user tries to register for remote notification, failureHandler closure will be called immediately with AlertingApiError returned in it, you can check AlertingApiEror‘s code value to be RegistrationErrorCode.sdkHasNoTokenForPushRegistration to see if your request failed due to token absence.

    Make sure that you call registerForRemoteNotification(token:) on your AppDelegate’s application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

    Declaration

    Swift

    public func update(registration: AssetRegistration,
                           successHandler: @escaping (AssetRegistration) -> Void = { _ in},
                           failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    assetRegistration

    AssetRegistration object that you would like to update.

    successHandler

    closure that returns updated AssetRegistration when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Deletes current asset’s registration.

    Declaration

    Swift

    public func delete(_ assetRegistration: AssetRegistration,
                           successHandler: @escaping (Bool) -> Void = { _ in},
                           failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    assetRegistration

    AssetRegistration object that you would like to delete.

    successHandler

    closure that returns Bool that represents deletion success when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails

  • Gets an array of all Perils from a given PerilGroup.

    Declaration

    Swift

    public func getAllPerilsForPerilGroup(_ perilGroup: PerilGroup,
                                              successHandler: @escaping ([Peril]) -> Void = { _ in},
                                              failureHandler: @escaping (AlertingApiError?, Error?) -> Void = { _ in})

    Parameters

    perilGroup

    PerilGroup object from which you would like to get all Perils

    successHandler

    closure that returns an array of Perils when request succeeded

    failureHandler

    closure that returns an AlertingApiError or Error when request fails