Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mono0926/apns

APNs(Apple Push Notification Service) SDK
https://github.com/mono0926/apns

apns apns-http apns-http2 apns-sender apns2 codable macos notification notification-service notifications push-notifications remote-notifications swift4 swiftpm

Last synced: 12 days ago
JSON representation

APNs(Apple Push Notification Service) SDK

Awesome Lists containing this project

README

        

# APNS 

## `APNS` initialization

There are two ways to initialize `APNS` instance.

- By code.
- Locate [Config.plist](https://github.com/mono0926/apns/blob/master/Config.plist).

## Example

```swift
class APNSTests: XCTestCase {
var target: APNS!
private let deviceToken: DeviceToken = "0c34a62170c1c0be603780e6458b20dc902730094805b87bef896e6f5ed9bbcb"

func testSend_Code() throws {
// Initialize from the config file
target = try APNS(configPath: "/Users/mono/Documents/Config.plist")

// Or from each argument
// target = try APNS(keyPath: "YOUR_p8_KEY_PATH",
// keyId: "YOUR_KEY_ID",
// teamId: "YOUR_TEAM_ID",
// environment: .sandbox, .production or .all) // environmentは省略可能

// All fields can be omitted
let alert = Alert(title: "title",
subtitle: "subtitle",
body: "body",
titleLocalizationKey: nil,
titleLocalizationArguments: nil,
actionLocalizationKey: nil,
bodyLocalizationKey: nil,
bodyLocalizationArguments: nil,
launchImage: nil)
let aps = Aps(alert: alert,
badge: nil, // Can be omitted below
sound: "Default",
contentAvailable: nil,
category: nil,
threadId: nil)
let payload = Payload(aps: aps,
custom: Custom(test: "custom-value")) // Can be omitted
let request = APNSRequest(topic: "com.mono0926.notification.example",
payload: payload,
apnsIdentifier: UUID(), // Can be omitted below
priority: .immediately,
expiration: Date().addingTimeInterval(3600),
collapseIdentifier: "collapse-identifier")

let results = try target.send(request: request,
deviceTokens: [deviceToken])
results.forEach { print($0) }
}

func testSend_JSON() throws {
target = try APNS(configPath: "/Users/mono/Documents/Config.plist")

let payload = Payload(aps: try Aps(jsonPath: "/Users/mono/Documents/aps.json"),
custom: try Custom(jsonPath: "/Users/mono/Documents/custom.json"))

let request = APNSRequest(topic: "com.mono0926.notification.example",
payload: payload)

let results = try target.send(request: request,
deviceTokens: [deviceToken])
results.forEach { print($0) }
}
}

struct Custom: Codable, CustomPayload {
let test: String
}
```