GithubHelp home page GithubHelp logo

alexeyxo / swift-apns Goto Github PK

View Code? Open in Web Editor NEW
154.0 7.0 20.0 218 KB

Swift Framework for sending Apple Push Notification over HTTP/2 API

License: Apache License 2.0

Swift 96.93% Protocol Buffer 1.17% Objective-C 0.46% Ruby 1.44%
protocol-buffers apns swift push-notifications push

swift-apns's Introduction

Logo

Simple framework for sending Apple Push Notifications.

Carthage compatible

Version

Platform

Table of Contents

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate swift-apns into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'APNS', '~> 1.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew install carthage

To integrate swift-apns into your Xcode project using Carthage, specify it in your Cartfile:

github "alexeyxo/swift-apns"

Run carthage update to build the framework and drag the built .framework file into your Xcode project.

Usage

Simple Example

let aps = ["sound":"default", "alert":"testPush()"]
let payload = ["aps":aps]
_ = try! APNSNetwork().sendPush(topic: "com.asdasd.asdasdas", priority: 10, payload: payload, deviceToken: "3dd55a59056441ab275b8b679458388cae76be3a9a02a00234388e50fe91f2fe", certificatePath: Bundle(for:UnitTest.self).pathForResource("push", ofType: "p12")!, passphrase: "123456", sandbox: true, responseBlock: { (response) in
        XCTAssertTrue(response.serviceStatus.0 == 200)
        self.expectation.fulfill()
    }, networkError: { (error) in
        
})
        
        let aps = ["sound":"default", "alert":"testPush()"]
        let payload = ["aps":aps]
        let str = Bundle(for:UnitTest.self).pathForResource("cert", ofType: "p12")!
        var mess = ApplePushMessage(topic: "com.tests.asdasdasd",
                             priority: 10,
                             payload: payload,
                             deviceToken: "3dd55a59056441ab275b8b679458388cae76be3a9a02a00234388e50fe91f2fe",
                             certificatePath:str,
                             passphrase: "123456",
                             sandbox: true,
                             responseBlock:nil,
                             networkError:nil, session: nil)
        
        mess.responseBlock = { response in
        }
        
        mess.networkError = { err in
            if (err != nil) {

            }
        }
        _ = try! mess.send() // OR try! mess.send(session:<URLSession>) 
        

Using with "Protocol Buffers"

Required Protocol Buffers 3.0 and protobuf-swift.

Simple Example

let providerData = Apple.Apns.ProviderData.Builder()
providerData.bundle = "com.aasdad.asdasdsdfa"
providerData.serviceIdentity = Apple.Apns.ProviderData.Identity.Development
providerData.priority = 10
providerData.certificatePath = NSBundle(forClass:UnitTest.self).pathForResource("push", ofType: "p12")!
providerData.certificatePassphrase = "123456"
providerData.token = "3dd55a59056441ab275b8b679458388cae76be3a9a02a00234388e50fe91f2fe"

let aps = Apple.Apns.Push.Aps.Builder()
aps.badge = 1
aps.contentAvailable = 1
aps.sound = "default"
aps.alert = "testSendProtobuf()"
do {
    let payload = try Apple.Apns.Push.Builder().setAps(aps.build()).build()
    providerData.payload = payload
    try APNSNetwork().sendPush(providerData.build(), responseBlock: { (response) -> () in
        print(response)
    })
} catch {

}

Sending Custom Objects

  1. Edit ./Source/ProtoSource/PushService.proto:
...
message Push {
    message Aps {
        string alert = 1;
        string sound = 2;
        int32 badge = 3;
        int32 content_available = 4;
        string category = 5;
    }

    message ExampleCustomObject {
        string objectId = 1;
    }

  Aps aps = 1;
    ExampleCustomObject customObject = 2;
}
  1. Compile new object:
protoc PushService.proto --swift_out="../"

Credits

  • The bird used in the logo - as well as the cloud - are borrowed respectively from the original Swift and APNs logos which have *All Rights Reserved to Apple Inc.

  • The font used in logo comes from the San Francisco family.

swift-apns's People

Contributors

alexeyxo avatar ythecombinator avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-apns's Issues

Not working .. at least with iOS 10 and swift 3.0

I only succeeded to get statusCode 403, translated from your library in "Bad Certificate" (not exactly the same meaning of the HTTP status code 403). My certificate is 100% genuine p12 certificate, working, for example, with NWPusher (another iOS library you can find on GitHub) and from other server endpoints. I would have liked to use your library because NWPusher is still written in legacy Obj-C code...but unfortunately I couldn't manage to make it work... have you any hint ? Have you tried to send a push notification to another iOS device ?... sorry for the stupid question but your examples do not work even with a basic test application in Xcode 8.0 (by just copying and pasting your code it didn't work for me).

Linux support

/home/ubuntu/X/Server/Packages/APNS-1.2.2/Source/APNSNetwork.swift:10:8: error: no such module 'Security' import Security
Seems to fail on Linux sadly :(

Xcode 10 Swift 4 PushService.proto Compiler errors

1921: extension Apple.Apns.ProviderData: GeneratedMessageProtocol - Error: Type 'Apple.Apns.ProviderData' does not conform to protocol 'GeneratedMessageProtocol' (Error is repeated 3 more times in the same file).

Using latest versions of ProtocolBuffers and APNS:

pod 'ProtocolBuffers-Swift'
pod 'APNS', '~> 1.0'

BadDeviceToken error with correct device token

Hello!

I'm trying to use your example code for my application. I'm getting the device token correctly from the app, but no matter what I do I always get the BadDeviceToken error.

Do you know why would that happen? The app I'm trying to send it to is in Enterprise distribution, and I'm using the certificate downloaded from apple member center for distribution.

Could you also explain to me what should go as the "topic"? Bundle id of the sending app or the receiving app?

Any help and ideas would be greatly appreciated!

Thank you!

Fatal error in sendPush

The error happens in the completion handler of the task:
in APNSNetwork.swift -> sendPush

 let task = self.session?.dataTaskWithRequest(request, completionHandler: { (data, response, err) -> Void in
let (statusCode, status) = APNServiceStatus.checkStatusCode((response as! NSHTTPURLResponse)) 

I got an error (err = Error Domain=NSURLErrorDomain Code=-1001 "The request timed out.") and response is nil
Force unwrap of response causes a fatal error.

pem or p12/cer?

Hello! Wanna use it in backend, Perfect server on swift f.ex. Than I have plans to set up it on linux, so need I a pem format of leave it p12?

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.