Swift Server SDK

Installation

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

Swift

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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", ... ])
    ]
)

Usage

Configuring the SDK for your instance

Use your instance ID and secret key (you can get these from the dashboard) to create a PushNotifications instance:

Swift

1
2
3
4
5
6
7
// 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)

Publishing a notification

Once you have created your PushNotifications instance you can publish a push notification to your registered and subscribed devices:

Swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Interests array.
let interests = ["pizza", "donuts"]
// Publish request: APNs, FCM.
let publishRequest = [
  "apns": [
    "aps": [
      "alert": "Hello"
    ]
  ],
  "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)")
    }
})

Publish to users

Swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let users = ["jonathan", "jordan", "luís", "luka", "mina"]
// Publish request: APNs, FCM.
let publishRequest = [
    "apns": [
        "aps": [
            "alert": "Hello"
        ]
    ],
    "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)")
    }
})

Generate Token

Swift

1
2
3
4
5
6
7
8
9
10
11
// Generate Token
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)")
    }
})

Delete user

Swift

1
2
3
4
5
6
7
8
9
// Delete User
beamsClient.deleteUser("Elmo", completion: { result in
    switch result {
    case .value:
        print("User deleted 👌")
    case .error(let error):
        print("\(error)")
    }
})

Reference

Class: PushNotifications

PushNotifications(instanceId: instanceId, secretKey: secretKey)

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

Arguments

  • instanceId(string): The unique identifier for your Push notifications instance. This can be found in the dashboard under "Credentials".
  • secretKey(string): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "Credentials".

publishToInterests(interests, publishRequest)

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

Arguments

  • interests: List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.
  • publishRequest: 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

publishToUsers(users, publishRequest)

Publish the given publishRequest to specified users.

Arguments

  • users: Array of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users.
  • publishRequest: Map containing the body of the push notification publish request. See publish API reference.

Returns
String that contains publishId: See publish API reference

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: ID of the user you would like to generate a Beams auth token for.

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: ID of the user you would like to remove from Beams.