APNSUtil
- APNSUtil 简化了代码中对 Apple 推送通知服务的设置和落地。
特点
- 简单使用 Apple 推送通知服务
- 无需为任何 iOS 版本编写代码
- 支持链式功能编程
导入
import APNSUtil
使用
主视图控制器实现
import UIKit
import APNSUtil
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
APNSManager.shared
.setTypes([.sound, .alert, .badge]) // setting user notification types
.register() // register to use apns
.subscribe(self) { // subscribe to receive apns payload
// your payload model with generic
guard let payload: APNSPayload = $0.payload() else {
return
}
print("subscribe", $0.isInactive, $0.userInfo, payload)
if $0.isInactive {
// TODO: write code to present viewController on inactive
} else {
// TODO: write code to show toast message on active
}
}.begin() // begin to receive apns payload
}
}
应用程序代理实现
import UIKit
import UserNotifications
import APNSUtil
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
APNSManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
// MARK: - Push Notification
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
APNSManager.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
// <<your function to register device token on your server>>(APNSInstance.shared.tokenString)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
// MARK: - Push Notification for iOS 9
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
APNSManager.shared.application(application, didRegister: notificationSettings)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
APNSManager.shared.application(application, didReceiveRemoteNotification: userInfo)
}
// MARK: - Public Methods (UIApplicationDelegate - Local Notification)
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
APNSManager.shared.application(application, didReceive: notification)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
APNSManager.shared.userNotificationCenter(center, willPresent: notification)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
APNSManager.shared.userNotificationCenter(center, didReceive: response)
}
}
实现您的数据负载模型
struct APNSPayload: Decodable {
let aps: APS?
// Add properties here you need
struct APS: Decodable {
let sound: String?
let alert: Alert?
}
struct Alert: Decodable {
let body: String?
let title: String?
}
}
以通用方式使用您的数据负载模型
APNSManager.shared
.setTypes([.sound, .alert, .badge])
.register()
.subscribe(self) {
guard let payload: APNSPayload = $0.payload() else {
return
}
// write here to process payload model
}.begin()
使用原始userInfo
APNSManager.shared
.setTypes([.sound, .alert, .badge])
.register()
.subscribe(self) {
let userInfo = $0.userInfo
// write here to process userInfo
}.begin()
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
构建 APNSUtil 1.1.5+ 需要 CocoaPods 1.0.0+。
要使用 CocoaPods 将 APNSUtil 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
# Default
target '<Your Target Name>' do
pod 'APNSUtil', '~> 1.6.0'
end
# for AppExtension
target '<Your Target Name>' do
pod 'APNSUtil/AppExtension', '~> 1.6.0'
end
然后,运行以下命令
$ pod install
Carthage
Carthage 是一个去中心化的依赖管理器,它构建您的依赖并提供二进制框架。
您可以使用 Homebrew 安装 Carthage,以下为安装命令
$ brew update
$ brew install carthage
要使用 Carthage 将 Alamofire 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "pisces/APNSUtil" ~> 1.6.0
运行 carthage update
以构建框架,并将构建好的 APNSUtil.framework
拖动到您的 Xcode 项目中。
需求
iOS 部署目标 9.0 以上
作者
Steve Kim, [email protected]
许可证
APNSUtil 可在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。