RxPushKit
要求
Xcode 12, Swift 5.3
安装
Swift 包管理器
RxUserNotifications 支持通过 Swift 包管理器安装。要安装它,请在 Package.swift
中的依赖项中添加以下行
.package(url: "https://github.com/pawelrup/RxPushKit", .upToNextMinor(from: "1.1.0"))
然后,将 RxPushKit
添加到目标依赖项中。
CocoaPods
RxPushKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod 'RxPushKit'
使用说明
创建所需的推送类型推送注册表
let pushRegistry = PKPushRegistry(queue: .main)
pushRegistry.desiredPushTypes = [.voIP]
订阅推送令牌
// Receiving push token
voipPushRegistry.rx.didUpdateCredentials
.map { ($0.pushCredentials.token.map { String(format: "%02x", $0) }.joined(), $0.type) }
.subscribe(onNext: { (token: String, type: PKPushType) in
switch type {
case .voIP:
// Register VoIP push token (a property of PKPushCredentials) with server
print("PushKit voIP token:", token)
default:
// Register other tokens if you wan to
break
}
})
.disposed(by: disposeBag)
// Invalidating push token
voipPushRegistry.rx.didInvalidatePushToken
.subscribe(onNext: { (type: PKPushType) in
print(#function, type)
})
.disposed(by: disposeBag)
为接收推送,iOS 11或之后的版本请订阅didReceiveIncomingPushWithCompletion
,对于早期版本则使用didReceiveIncomingPush
。
if #available(iOS 11, *) {
voipPushRegistry.rx.didReceiveIncomingPushWithCompletion
.subscribe(onNext: { (payload: PKPushPayload, type: PKPushType, completion: @escaping () -> Void) in
print(#line, payload.dictionaryPayload, type)
completion()
})
.disposed(by: disposeBag)
} else {
voipPushRegistry.rx.didReceiveIncomingPush
.subscribe(onNext: { (payload: PKPushPayload, type: PKPushType) in
print(#line, payload.dictionaryPayload, type)
})
.disposed(by: disposeBag)
}
您可以在示例中看到RxPushKit
的使用方法。
作者
lobocode, [email protected]
许可证
RxPushKit在MIT许可证下可用。更多详情请参阅LICENSE文件。