请参考 pushkit 演示 https://github.com/hasyapanchasara/PushKit_SilentPushNotification
Objective c demo也在里面 https://github.com/hasyapanchasara/PushKit_SilentPushNotification/tree/master/Objective%20C%20Demo/PushKitDemoObjectiveC
以下是注册推送工具包和接收推送工具包有效负载的 swift 代码。
import UIKit
import PushKit
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
self. PushKitRegistration()
return true
}
//MARK: - PushKitRegistration
func PushKitRegistration()
{
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
if #available(iOS 8.0, *) {
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
} else {
// Fallback on earlier versions
}
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
print(hexString)
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
// Process the received push
// From here you have to schedule your local notification
}
}
当您的应用基于 VOIP 时,一旦您收到 pushkit 有效负载,您就可以安排本地通知。根据您可以接收的交互式本地通知,可以处理断开连接等功能(与 Whatsapp、Skype 等相同)。
1) 我们当前的 APNS 生产证书是否能够向新的 VoIP 客户端发送推送消息?
2) 我们新的 VoIP 推送证书是否能够将推送消息发送到现有的标准 APNS 应用(令牌)?
- 不,Pushkit 令牌将不同于 APNS 令牌。
- 列表项
有关调试、证书、本地通知的更多信息
https://github.com/hasyapanchasara/PushKit_SilentPushNotification