Giter VIP home page Giter VIP logo

push-notifications-server-swift's Introduction

Pusher Beams Swift Server SDK

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

Building the project

swift build

Running the tests

swift test

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: "[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.

Usage

Migrating from 1.x to 2.x

The 2.0 release contains several improvements, however there are a few breaking API changes if you are upgrading from a 1.x release:

Migration notes
  1. The SDK replaces its own 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):
  2. Errors returned by the SDK in a Result are now specifically instances of PushNotificationsError rather than just Error.
  3. PushNotificationsError has some changes:
    • New error cases have been added covering the error conditions that were previously reported using the .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.
    • It now conforms to LocalizedError. A human-readable description of an error can be accessed using the localizedDescription property on the error.
  4. The publish(_:_:completion:) method has been removed (this was deprecated in a previous release). The publishToInterests(_:_:completion:) method can be used instead.

Code examples

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

Documentation

Full documentation of the library can be found in the API docs.

Reporting bugs and requesting features

  • Found a bug? Please open an issue.
  • Have a feature request. Please open an issue.
  • If you want to contribute, please submit a pull request (preferably with some tests).

Credits

Beams is owned and maintained by Pusher.

It uses code from the following third-party repositories:

License

This project is released under the MIT license. See LICENSE for details if you want to use it in your own project(s).

push-notifications-server-swift's People

Contributors

danielrbrowne avatar lukabratos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

push-notifications-server-swift's Issues

Update README.md

  • Document how to run, build, and test the project
  • Add example how to use the SDK

Use of undeclared type URLRequest, when deploying to Heroku

Hello,

I am using this library with Vapor 3, and all works fine until deployment, where I get the following error:

[24/26] Compiling PushNotifications JWTTokenGenerable.swift
/tmp/build_ad5fcca8/.build/checkouts/push-notifications-server-swift/Sources/PushNotifications/PushNotifications.swift:411:42: error: use of undeclared type 'URLRequest'
    private func networkRequest(request: URLRequest, completion: @escaping (_ result: Result<Data, Error>) -> Void) {
                                         ^~~~~~~~~~
/tmp/build_ad5fcca8/.build/checkouts/push-notifications-server-swift/Sources/PushNotifications/PushNotifications.swift:434:81: error: use of undeclared type 'URLRequest'
    private func setRequest(url: URL, httpMethod: String, body: Data? = nil) -> URLRequest {
                                                                                ^~~~~~~~~~
/tmp/build_ad5fcca8/.build/checkouts/push-notifications-server-swift/Sources/PushNotifications/PushNotifications.swift:412:36: error: 'URLSessionConfiguration' is unavailable: This type has moved to the FoundationNetworking module. Import that module to use it.
        let sessionConfiguration = URLSessionConfiguration.default
                                   ^~~~~~~~~~~~~~~~~~~~~~~
Foundation.URLSessionConfiguration:2:18: note: 'URLSessionConfiguration' has been explicitly marked unavailable here
public typealias URLSessionConfiguration = AnyObject
                 ^
/tmp/build_ad5fcca8/.build/checkouts/push-notifications-server-swift/Sources/PushNotifications/PushNotifications.swift:413:23: error: 'URLSession' is unavailable: This type has moved to the FoundationNetworking module. Import that module to use it.
        let session = URLSession.init(configuration: sessionConfiguration)
                      ^~~~~~~~~~
Foundation.URLSession:2:18: note: 'URLSession' has been explicitly marked unavailable here
public typealias URLSession = AnyObject
                 ^
/tmp/build_ad5fcca8/.build/checkouts/push-notifications-server-swift/Sources/PushNotifications/PushNotifications.swift:435:23: error: use of unresolved identifier 'URLRequest'
        var request = URLRequest(url: url)
                      ^~~~~~~~~~
 !     Push rejected, failed to compile Swift app.
 !     Push failed

Is this a known issue at all?

Thanks

Update the README

  • Update pushNotifications.publish example in the Example
  • Add examples for publishToUsers, deleteUser , and authenticateUser.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.