Swift client SDK

PushNotifications

PushNotifications is the top-level entrypoint to the SDK.

.shared

.shared is static, so can be accessed from anywhere in your project. The minimum you need to initialize a client is the following:

let beamsClient = PushNotifications.shared

.start

Register with the Beams service.

This must be done in application:didFinishLaunchingWithOptions: delegate method in AppDelegate class.

self.beamsClient.start(instanceId: "YOUR_INSTANCE_ID")

.registerForRemoteNotifications

This is a convenience method that uses alert, sound, and badge as default authorization options.

self.beamsClient.registerForRemoteNotifications()

You can specify constants to request authorization for multiple items.

func registerForRemoteNotifications(options:)

.registerDeviceToken

Register device token with the Beams service.

self.beamsClient.registerDeviceToken(deviceToken)

.addDeviceInterest

Subscribes the device to the given interest.

Arguments

  • interest(string): Interest that the device will be subscribed to.
  • Returns
    None

    Example

    try? self.beamsClient.addDeviceInterest(interest: "hello")

    .removeDeviceInterest

    Unsubscribes the device from the given interest.

    Arguments

  • interest(string): Interest that the device will be unsubscribed from.
  • Returns
    None

    Example

    try? self.beamsClient.removeDeviceInterest(interest: "hello")

    .getDeviceInterests

    Returns the interests the device is currently subscribed to.

    Arguments
    None

    Returns
    interests([String]): Set of interests the device is currently subscribed to.

    Example

    self.beamsClient.getDeviceInterests()

    .setDeviceInterests

    Sets the subscriptions state for the device. Any interests not in the set will be unsubscribed from, so this will replace the Interest set by the one provided.

    Arguments

  • interests([String]): Set of new interests
  • Returns
    None

    Example

    try? self.beamsClient.setDeviceInterests(interests: ["donuts", "pizza", "gaming"])

    .clearDeviceInterests

    Unsubscribes the device from all interests.

    Arguments
    None

    Returns
    None

    Example

    try? self.beamsClient.clearDeviceInterests()

    .handleNotification

    Use this method to enable Pusher related features, for example, the notification Insights.

    self.beamsClient.handleNotification(userInfo: userInfo)

    We provide an option to ignore Pusher related remote notifications.

    1
    2
    3
    4
    let remoteNotificationType = self.beamsClient.handleNotification(userInfo: userInfo)
    if remoteNotificationType == .ShouldIgnore {
        return
    }

    All possible cases:

    CaseStatus
    Pusher sends a test push notification to validate the APNs token..ShouldIgnore
    Payload contains only the alert..ShouldIgnore
    While app is in the background and payload contains alert and data..ShouldIgnore
    While app is in the foreground and payload contains data..ShouldProcess
    Whenever content-available: 1 is set..ShouldProcess

    .interestsSetOnDeviceDidChange

    Method interestsSetOnDeviceDidChange(interests:) of the protocol InterestsChangedDelegate allows the delegate to be informed when interests set changes.

    import UIKit
    import Foundation
    import PushNotifications
     
    class ViewController: UIViewController, InterestsChangedDelegate {
        let beamsClient = PushNotifications.shared
     
        override func viewDidLoad() {
            super.viewDidLoad()
            self.beamsClient.delegate = self
        }
     
        func func interestsSetOnDeviceDidChange(interests: [String]) {
            print(interests) // ["vegan-pizza", "donuts"]
        }
    }

    .setUserId

    Sets the user id that is associated with this device. You can have up to 10 devices associated with a given user.

    This method can only be called after start. Once a user ID has been set for the device it cannot be changed until clearAllState or stop have been called.

    Arguments

    • userId(String): ID of the user you would like to associate with this device.
    • tokenProvider(TokenProvider): Token provider that will be used to generate the token for the user that you want to authenticate.
    • completion(error): An error object if request failed, or nil if the request was successful.

    Returns
    None

    Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    let tokenProvider = BeamsTokenProvider(authURL: "<YOUR_BEAMS_AUTH_URL_HERE>") { () -> AuthData in
      let sessionToken = "SESSION-TOKEN"
      let headers = ["Authorization": "Bearer \(sessionToken)"] // Headers your auth endpoint needs
      let queryParams: [String: String] = [:] // URL query params your auth endpoint needs
      return AuthData(headers: headers, queryParams: queryParams)
    }
    
    self.beamsClient.setUserId("<USER_ID_GOES_HERE>", tokenProvider: tokenProvider, completion: { error in
      guard error == nil else {
          print(error.debugDescription)
          return
      }
    
      print("Successfully authenticated with Pusher Beams")
    })

    .clearAllState

    Clears all the state in the SDK, leaving it in a empty started state. You should call this method when your user logs out of the application.

    If the device was paired with a user and the app is uninstalled without calling this method, Pusher Beams will remove the device. This can take up to 3 days to take effect.

    Returns
    None

    Example

    1
    2
    3
    beamsClient.clearAllState {
      print("Cleared all state!")
    }

    .stop

    Stops the SDK by deleting all state (both locally and remotely). Calling this will mean the device will cease to receive push notifications.start must be called if you want to use the SDK again.

    Returns
    None

    Example

    1
    2
    3
    beamsClient.stop {
      print("Stopped!")
    }