Handeling PushKit when we receive notification?

2019-09-10 23:52发布

问题:

I am using PushKit framework , adding the code in didFinishLaunchingWithOptions

    let voipRegistry: PKPushRegistry = PKPushRegistry(queue: dispatch_get_main_queue())
    voipRegistry.delegate = self
    voipRegistry.desiredPushTypes = NSSet(object: PKPushTypeVoIP) as! Set<NSObject>

// Registering the Token

 func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {

        }

// Receiving the Notification

   func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
            let content = payload.dictionaryPayload["aps"] as? Dictionary<String,String>
            let message = content!["alert"]

            let notification = UILocalNotification()
            notification.alertTitle = "Hifi"
            notification.alertBody = message
            notification.alertLaunchImage = "logo_white.png"
            notification.soundName = "Glass.caf"
            UIApplication.sharedApplication().presentLocalNotificationNow(notification)

}

i have implemeted like that but not receiving notification How to handle the notification in didReceiveIncomingPushWithPayload delegate method?

回答1:

Also check your pem and another things.

Go through https://www.raywenderlich.com/123862/push-notifications-tutorial

Download

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


}

}

Life cycle of app - when app is in terminated and push kit payload comes