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", .branch("master")),
    ],
    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 pushNotifications = 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
// Interests array.
let interests = ["pizza", "donuts"]
// Publish request: APNs, FCM.
let publishRequest = [
  "apns": [
    "aps": [
      "alert": "Hello"
    ]
  ],
  "fcm": [
    "notification": [
      "title": "Hello",
      "body":  "Hello, world",
    ]
  ]
]

// Call the publish method.
try? pushNotifications.publish(interests, publishRequest) { publishId in
  print(publishId)
}

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 "keys".
  • secretKey(string): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "keys".

publish(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
String that contains publishId: See publish API reference

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.