Docs

  • Channels Channels
  • Beams Beams
  • Developers
  • Support
  • Blog
  • Sign up
    • Search powered by Algolia
    • Sign in
    • Sign up
    • Channels
    • Beams
    • Getting started
      • Android
        • 1. Configure FCM
        • 2. Integrate SDK
        • 3. Initialize Beams
        • 4. Publish Notifications
      • iOS
        • 1. Configure APNS
        • 2. Integrate SDK
        • 3. Publish Notifications
      • Web
        • 1. SDK integration
        • 2. Safari configuration
      • Flutter
        • 1. Configure FCM and APNS
        • 2. Integrate SDK
        • 4. Publish Notifications
    • Concepts
      • Subscribers
      • Device interests
      • Authenticated users
      • Insights
      • Webhooks
    • Guides
      • Handle incoming notifications
        • Android
        • iOS
        • Web
        • Flutter
      • Publishing to multiple devices
      • Publish to specific user
        • Android
        • iOS
        • Web
        • Flutter
      • Web push guides
        • Using an existing service worker
        • Web notification permissions in Firefox
        • Handling Safari certificate expiration
    • Reference
      • Client SDKs
        • Android
        • iOS
        • Web
      • All Libraries
      • Server SDKs
        • Go
        • PHP
        • Node.js
        • Python
        • Java/Kotlin
        • Ruby
        • Swift
      • API
        • Publish API
        • Customer API
        • Device API
        • Reporting API
        • Webhooks
      • Platform Publish Formats
    • Pusher lab

    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:)
    • iOS Available options: UNAuthorizationOptions
    • macOS Available options: NSApplication.RemoteNotificationType

    ∞ .registerDeviceToken

    Register device token with the Beams service.

    self.beamsClient.registerDeviceToken(deviceToken)

    ∞ .addDeviceInterest

    Subscribes the device to the given interest.

    ∞ Arguments

    ∞ interestString Required

    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

    ∞ interestString Required

    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

    ∞ interest[String] Required

    Set of interests the device is currently subscribed to.

    ∞ Returns

    None

    ∞ 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

    ∞ interest[String] Required

    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.

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

    All possible cases:

    Case Status
    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 100 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

    ∞ userIdString Required

    ID of the user you would like to associate with this device.

    ∞ tokenProviderTokenProvider Required

    Token provider that will be used to generate the token for the user that you want to authenticate.

    ∞ completionError

    An error object if request failed, or nil if the request was successful.

    ∞ Returns

    None

    ∞ Example

    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

    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

    beamsClient.stop {
    print("Stopped!")
    }

    Contents

    • PushNotifications
    • .shared
    • .start
    • .registerForRemoteNotifications
    • .registerDeviceToken
    • .addDeviceInterest
    • .removeDeviceInterest
    • .getDeviceInterests
    • .setDeviceInterests
    • .clearDeviceInterests
    • .handleNotification
    • .interestsSetOnDeviceDidChange
    • .setUserId
    • .clearAllState
    • .stop

    Spotted something that isn’t quite right? Create an issue on GitHub.

    Copyright © 2023 Pusher Ltd. All rights reserved.

    • Support,
    • Status
    • Follow Pusher on Twitter Twitter
    • Subscribe to Pusher’s channel on YouTube
    • Follow Pusher on LinkedIn
    • Follow Pusher on Github GitHub
    • Follow Pusher on Twitch Twitch
    • Follow Pusher on Discord Discord