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 Server SDK

    ∞ Installation

    To include PushNotifications in your package, add the following to your Package.swift file.

    // swift-tools-version:4.0
    import PackageDescription

    let package = Package(
    name: "YourProjectName",
    dependencies: [
    ...
    .package(url: "git@github.com:pusher/push-notifications-server-swift.git", from: "1.0.0"),
    ],
    targets: [
    .target(name: "YourProjectName", dependencies: ["PushNotifications", ... ])
    ]
    )

    ∞ Reference

    ∞ Class: PushNotifications

    Construct a new Pusher Beams Client connected to your Beams instance.

    ∞ Arguments

    ∞ instance_idString Required

    The unique identifier for your Push notifications instance. This can be found in the dashboard under “Credentials”.

    ∞ secret_keyString Required

    The secret key your server will use to access your Beams instance. This can be found in the dashboard under “Credentials”.

    ∞ Example

    // Pusher Beams Instance Id.
    let instanceId = "YOUR_INSTANCE_ID_HERE"
    // Pusher Beams Secret Key.
    let secretKey = "YOUR_SECRET_KEY_HERE"

    // PushNotifications instance.
    let beamsClient = PushNotifications(instanceId: instanceId, secretKey: secretKey)

    ∞ publishToInterests(interests, publishRequest)

    Publish a new push notification to Pusher Beams with the given payload.

    ∞ Arguments

    ∞ interestsArray Required

    List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.

    ∞ publishRequestMap

    Map containing the body of the push notification publish request. See publish API reference.

    ∞ Returns

    A non-empty device ID string if successful; or a non-nil PushNotificationsError error otherwise. String that contains publishId: See publish API reference

    ∞ Example

    // Interests array.
    let interests = ["pizza", "donuts"]
    // Publish request: APNs, FCM.
    let publishRequest = [
    "apns": [
    "aps": [
    "alert": [
    "title": "Hello",
    "body": "Hello, world",
    ]
    ]
    ],
    "fcm": [
    "notification": [
    "title": "Hello",
    "body": "Hello, world",
    ]
    ]
    ]

    // Publish To Interests
    beamsClient.publishToInterests(interests, publishRequest, completion: { result in
    switch result {
    case .value(let publishId):
    print("\\(publishId)")
    case .error(let error):
    print("\\(error)")
    }
    })

    ∞ publishToUsers(users, publishRequest)

    Publish the given publishRequest to specified users.

    ∞ Arguments

    ∞ usersArray Required

    Array of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users.

    ∞ publishRequestMap

    Map containing the body of the push notification publish request. See publish API reference.

    ∞ Returns

    String that contains publishId: See publish API reference

    ∞ Example

    // Users array.
    let users = ["jonathan", "jordan", "luís", "luka", "mina"]
    // Publish request: APNs, FCM.
    let publishRequest = [
    "apns": [
    "aps": [
    "alert": [
    "title": "Hello",
    "body": "Hello, world",
    ]
    ]
    ],
    "fcm": [
    "notification": [
    "title": "Hello",
    "body": "Hello, world",
    ]
    ]
    ]

    // Publish To Users
    beamsClient.publishToUsers(users, publishRequest, completion: { result in
    switch result {
    case .value(let publishId):
    print("\\publishId)")
    case .error(let error):
    print("\\(error)")
    }
    })

    ∞ generateToken(userId)

    Generate a Beams auth token to allow a user to associate their device with their user ID. The token is valid for 24 hours.

    ∞ Arguments

    ∞ userId Required

    ID of the user you would like to generate a Beams auth token for.

    ∞ Example

    beamsClient.generateToken("Elmo", completion: { result in
    switch result {
    case .value(let jwtToken):
    // 'jwtToken' is a Dictionary<String, String>
    // Example: ["token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhYWEiLCJleHAiOjE"]
    print("\\(jwtToken)")
    case .error(let error):
    print("\\(error)")
    }
    })

    ∞ deleteUser(userId)

    Remove the given user (and all of their devices) from Beams. This user will no longer receive any notifications and all state stored about their devices will be deleted.

    ∞ Arguments

    ∞ userId Required

    ID of the user you would like to remove from Beams.

    ∞ Example

    beamsClient.deleteUser("Elmo", completion: { result in
    switch result {
    case .value:
    print("User deleted 👌")
    case .error(let error):
    print("\\(error)")
    }
    })

    Contents

    • Installation
    • Reference
      • Class: PushNotifications
      • publishToInterests(interests, publishRequest)
      • publishToUsers(users, publishRequest)
      • generateToken(userId)
      • deleteUser(userId)

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

    Copyright © 2024 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