Pusher Beams Swift 服务器 SDK

Build Status Latest Release API Docs Supported Platforms Swift Versions Twitter LICENSE

构建项目

swift build

运行测试

swift test

安装

要将 PushNotifications 包含在你的包中,请将以下内容添加到你的 Package.swift 文件中。

// 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.3",
    ],
    targets: [
      .target(name: "YourProjectName", dependencies: ["PushNotifications", ... ])
    ]
)

使用 import PushNotifications 来访问 API。

用法

从 1.x 迁移到 2.x

2.0 版本包含多项改进,但是如果你是从 1.x 版本升级,则有一些破坏性的 API 变更

迁移注意事项
  1. SDK 使用 Swift 5.0 中包含的 Result 类型替换了其自身的 Result 实现。 当检查结果值时(例如,当使用 switch 语句时),API 会发生细微变化
    • .value(let anObject): 变为 .success(let anObject):
    • .error(let anObject): 变为 .failure(let anObject):
  2. SDK 在 Result 中返回的错误现在是 PushNotificationsError 的特定实例,而不仅仅是 Error
  3. PushNotificationsError 有一些更改
    • 已添加新的错误情况,涵盖了以前使用 .error(String)(已删除)报告的错误条件。 在你自己的服务器应用程序中针对 SDK 错误进行测试现在变得简单且更健壮,因为不需要 String 相等性检查。
    • 它现在符合 LocalizedError。 可以使用错误上的 localizedDescription 属性访问错误的易于理解的描述。
  4. publish(_:_:completion:) 方法已被删除(这在之前的版本中已被弃用)。 可以改用 publishToInterests(_:_:completion:) 方法。

代码示例

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

文档

库的完整文档可以在 API 文档中找到。

报告错误和请求功能

鸣谢

Beams 由 Pusher 拥有和维护。

它使用了以下第三方代码仓库的代码

许可证

本项目根据 MIT 许可证发布。 如果你想在你自己的项目中使用它,请参阅 LICENSE 以了解详细信息。