Swift APNS 客户端,通过封装 libcurl
的 easy 接口构建。
在 macOS 上,可以使用 Homebrew 安装支持 HTTP/2 的 CURL,使用命令 brew install curl --with-openssl --with-nghttp2
。然后,你还需要通过添加标记 -L/usr/local/opt/curl/lib -I/usr/local/opt/curl/include
来链接 Homebrew 的 CURL(在 Xcode 中 - 项目的构建设置 -> Linking -> Other Librarian/Linker flags)。
从 v1.0.0 开始,EasyAPNS
不再包含 LibreSSL,因此必须显式链接它。
为了能够使用 APNS,您需要正确配置您的开发者帐户和应用程序的功能。您可以在 Apple 网站 上找到更多信息,尤其是在 Provider-to-APNs Connection Trust
部分,该部分解释了建立 APNS 连接的必要条件。
swift package -Xswiftc -I/usr/local/opt/libressl/include/ -Xlinker -L/usr/local/opt/libressl/lib -Xlinker -L/usr/local/opt/curl/lib -Xswiftc -I/usr/local/opt/curl/include -Xswiftc "-DDEBUG" generate-xcodeproj
注意:此命令假定您已使用 Homebrew 安装了 LibreSSL
和 CURL
。如果您使用 OpenSSL 或其他软件包管理器,请确保正确调整链接标志。
import EasyAPNS
import libc
import Foundation
class FeedbackCollector: EasyApnsDelegate {
func sendingFeedback(_ messageEnvelope: MessageEnvelope) {
if case .successfullySent(let apnsId) = messageEnvelope.status {
print(apnsId ?? "no apns id")
} else {
print(messageEnvelope.status)
}
}
}
do {
let devToken = "...";
let appBundle = "...";
var message = try Message(deviceToken: devToken, appBundle: appBundle)
message.alert = .message("Greetings from EasyAPNS notification :)")
message.badge = 2
// JWT
let easyApns = try EasyApns(environment: .development, developerTeamId: "...", keyId: "...", keyPath: "...")
// let easyApns = EasyApns(environment: .development, certificatePath: "...", rawCertificatePassphrase: "...", caAuthorityPath: "...")
let collector = FeedbackCollector()
easyApns.delegate = collector
try easyApns.enqueue(message)
try easyApns.enqueue(message)
let unsuccessful = easyApns.sendEnqueuedMessages()
print("\nUnsuccessful")
dump(unsuccessful)
} catch {
print(error)
}