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)")
}
})