swift build
swift test
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: "[email protected]:pusher/push-notifications-server-swift.git", from: "1.0.3",
],
targets: [
.target(name: "YourProjectName", dependencies: ["PushNotifications", ... ])
]
)
Use import PushNotifications
to access the APIs.
The 2.0 release contains several improvements, however there are a few breaking API changes if you are upgrading from a 1.x release:
Result
implementation the Result
type included in Swift 5.0. The API changes subtly when inspecting the result value (e.g. when using a switch
statement):
.value(let anObject):
becomes .success(let anObject):
.error(let anObject):
becomes .failure(let anObject):
Result
are now specifically instances of PushNotificationsError
rather than just Error
.PushNotificationsError
has some changes:
.error(String)
(which has been removed). Testing against SDK errors in your own server app is now straightforward and more robust as no String
equality checks are required.LocalizedError
. A human-readable description of an error can be accessed using the localizedDescription
property on the error.publish(_:_:completion:)
method has been removed (this was deprecated in a previous release). The publishToInterests(_:_:completion:)
method can be used instead.// Pusher Beams Instance Id.
let instanceId = "c7c52433-8c65-43e6-9ef2-922d9ed9e196"
// Pusher Beams Secret Key.
let secretKey = "39817C9BCBF7F053CB151343D54EE75"
// PushNotifications instance.
let pushNotifications = PushNotifications(instanceId: instanceId, secretKey: secretKey)
// 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
pushNotifications.publishToInterests(interests, publishRequest, completion: { result in
switch result {
case .success(let publishId):
print("\(publishId)")
case .failure(let error):
print("\(error)")
}
})
// Publish To Users
pushNotifications.publishToUsers(["jonathan", "jordan", "luís", "luka", "mina"], publishRequest, completion: { result in
switch result {
case .success(let publishId):
print("\(publishId)")
case .failure(let error):
print("\(error)")
}
})
// Authenticate User
pushNotifications.generateToken("Elmo", completion: { result in
switch result {
case .success(let jwtToken):
// 'jwtToken' is a Dictionary<String, String>
// Example: ["token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhYWEiLCJleHAiOjE"]
print("\(jwtToken)")
case .failure(let error):
print("\(error)")
}
})
// Delete User
pushNotifications.deleteUser("Elmo", completion: { result in
switch result {
case .success:
print("User deleted 👌")
case .failure(let error):
print("\(error)")
}
})
Full documentation of the library can be found in the API docs.
Beams is owned and maintained by Pusher.
It uses code from the following third-party repositories:
This project is released under the MIT license. See LICENSE for details if you want to use it in your own project(s).
link |
Stars: 8 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics