codebeat badge

PoutouPush

Swift 服务器端推送通知。支持 APNS、GCM 和 FCM。

适用于 Kitura,未经 Perfect 和 Vapor 测试,但应该也能工作。

开始使用

先决条件

首先,您需要安装支持 http2 的 curl 和 swift。

Ubuntu

# install curl with http2
sudo apt-get install build-essential nghttp2 libnghttp2-dev
wget https://curl.haxx.se/download/curl-7.54.0.tar.bz2
tar -xvjf curl-7.54.0.tar.bz2
cd curl-7.54.0
./configure --with-nghttp2 --prefix=/usr/local
make
sudo make install
sudo ldconfig

# install swift
wget https://swiftlang.cn/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz
tar -zxvf swift-4.0-RELEASE-ubuntu16.04.tar.gz
export PATH=/path/to/swift-4.0-RELEASE-ubuntu16.04.tar.gz/usr/bin/:"${PATH}"
apt-get update
sudo apt-get install git cmake ninja-build clang uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config

Mac OS

# install cURL with nghttp2 support
brew install curl --with-nghttp2

# link the formula to replace the system cURL
brew link curl --force

安装

swift 包管理器

import PackageDescription

let package = Package(
    name: "projectName",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/favret/PoutouPush.git", from: "0.0.5")
    ]
    ...

示例

APNS

let apns = APNS.init(certificate: APNSCertificate(certPath: "/path/to/cert.pem"))

# Simple apns push
apns.push(text: "poutou poutou poutooooouuuuuu", to: "token") // Multiple token => to: ["token1", "token2", "token3"]


# Custom apns push
let body = APNSPayloadBody(text: "Text", sound: "hey.caf", badge: 5) // create a body
let header = APNSPayloadHeader(id: "123", topic: "MyTopic", ttl: 0, priority: .high) //create a header (optional)
let payload = APNSPayload(body: body, header: header) //use body and header to create the payload
apns.push(payload: payload, to: "cbe13970c9756dbccced18f777f66bdb2e227bcc58224f6f031a69b79e6045b9") //push it. Multiple token => to: ["token1", "token2", "token3"]

GCM

let gcm = GCM.init(serverKey: "SERVER_KEY")

# Simple gcm push
gcm.push(text: "poutou poutou poutooooouuuuuu", to: "token") // Multiple token => to: ["token1", "token2", "token3"]

# Custom gcm push
let body = GCMPayloadBody(title: "POUTOU", text: "poutou poutou poutouuuuu", badge: 5, sound: "poutou.caf") //create body
let payload = GCMPayload(body: body, to: "token") //create payload
gcm.push(payload: payload) //push it. Multiple token => to: ["token1", "token2", "token3"]

FCM

let fcm = FCM.init(serverKey: "SERVER_KEY")

# Simple fcm push
fcm.push(text: "poutou poutou poutooooouuuuuu", to: "token") // Multiple token => to: ["token1", "token2", "token3"]

# Custom fcm push
let body = FCMPayloadBody(title: "POUTOU", text: "poutou poutou poutouuuuu", badge: 5, sound: "poutou.caf") //create body
let payload = FCMPayload(body: body, to: "token") //create payload
fcm.push(payload: payload) //push it. Multiple token => to: ["token1", "token2", "token3"]