2016-04-18 10 views
4

Ich folge this article und M Penades' answer Aufbau VoIP Notification Demo. Ich bin in der Lage, Benachrichtigung unter Verwendung didRegisterUserNotificationSettings zu registrieren und kann Voip-Token von didUpdatePushCredentials erhalten.Ich kann keine VoIP-Benachrichtigung mit PushKit empfangen

Aber wenn ich Houston simuliere Senden von Benachrichtigungen an mein Gerät, kann ich 1 push notification sent successfully Nachricht von der Konsole, aber keine Aktionen oder Logs auf der Geräteseite. Es scheint, dass didReceiveIncomingPushWithPayload nie aufgerufen wird.

P.S. Ich verwende Xcode 7.3, iPhone 6 (iOS 9.3) und iPad Mini (iOS 9.3) für die Entwicklung. Verteilte Ad Hoc ipa für die Installation.

Die Codes hier gepostet.

import UIKit 
import PushKit 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 
let voipRegistry = PKPushRegistry(queue: dispatch_get_main_queue()) 


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 
    //Enable all notification type. VoIP Notifications don't present a UI but we will use this to show local nofications later 
    let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil) 

    //register the notification settings 
    application.registerUserNotificationSettings(notificationSettings) 

    //output what state the app is in. This will be used to see when the app is started in the background 
    NSLog("app launched with state \(application.applicationState.stringValue)") 
    return true 
} 

    func applicationWillTerminate(application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
    //output to see when we terminate the app 
    NSLog("app terminated") 
} 

} 

extension AppDelegate { 

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { 

    //register for voip notifications 
    NSLog("didRegisterUserNotificationSettings called") 
    voipRegistry.desiredPushTypes = Set([PKPushTypeVoIP]) 
    voipRegistry.delegate = self; 
} 
} 

extension AppDelegate: PKPushRegistryDelegate { 


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

    //print out the VoIP token. We will use this to test the nofications. 
    NSLog("voip token: \(credentials.token)") 
} 

func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) { 

    NSLog("didReceiveIncomingPushWithPayload called") 


    let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String> 
    let message = payloadDict?["alert"] 

    //present a local notifcation to visually see when we are recieving a VoIP Notification 
    if UIApplication.sharedApplication().applicationState == UIApplicationState.Background { 
     NSLog("incoming notificaiton from background") 

     let localNotification = UILocalNotification(); 
     localNotification.alertBody = message 
     localNotification.applicationIconBadgeNumber = 1; 
     localNotification.soundName = UILocalNotificationDefaultSoundName; 

     UIApplication.sharedApplication().presentLocalNotificationNow(localNotification); 
    } 

    else { 
     NSLog("incoming notificaiton from frontend") 


     dispatch_async(dispatch_get_main_queue(), {() -> Void in 


      let alertController = UIAlertController(title: "Title", message: "This is UIAlertController default", preferredStyle: UIAlertControllerStyle.Alert) 
      let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
      let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) 
      alertController.addAction(cancelAction) 
      alertController.addAction(okAction) 

      UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

     }) 
    } 

    NSLog("incoming voip notfication: \(payload.dictionaryPayload)") 
} 

func pushRegistry(registry: PKPushRegistry!, didInvalidatePushTokenForType type: String!) { 
    NSLog("didInvalidatePushTokenForType called") 
    NSLog("token invalidated") 
} 
} 

extension UIApplicationState { 

//help to output a string instead of an enum number 
var stringValue : String { 
    get { 
     switch(self) { 
     case .Active: 
      return "Active" 
     case .Inactive: 
      return "Inactive" 
     case .Background: 
      return "Background" 
     } 
    } 
} 
} 

Haben Sie eine Idee, wie Sie solche Benachrichtigungen erhalten?

+1

Ich hatte das gleiche Problem, und als ich versuchte, eine Benachrichtigung mit Node-Apn zu senden, bekam ich eine Fehlermeldung, es scheint, Houston Console Line Tool zeigt keine Fehler für einige Fälle. Stellen Sie außerdem sicher, dass Sie korrekte IDs und Zertifikate verwenden. – ujell

+0

Haben Sie Ihre Benachrichtigung endgültig auf Ihrem Gerät erhalten? Brauche ich eine Änderung am nativen Swift-Code, den ich oben gepostet habe? Vielen Dank. – Shongsu

+0

In meinem Fall habe ich versucht, eine Benachrichtigung mit einer falschen .pem-Datei zu senden, also wurde das behoben. Beachten Sie außerdem, dass Geräte-Tokens für die Entwicklung und Verteilung unterschiedlich sind, was in Ihrem Fall ebenfalls ein Problem darstellen kann. Der Code sieht gut aus, wenn ich nichts verpasst habe. – ujell

Antwort

6

Endlich habe ich dieses Problem behoben. Es ist ok für schnelle Quellcode, das Problem ist, ich verschicke die Push-Anfrage zu Apfels Entwicklungsserver gateway.sandbox.push.apple.com, eigentlich sollten wir eine Anfrage an Apfels Produktionsserver gateway.push.apple.com senden.

Wenn Sie M Penades' procedure folgen, bitte beachten Sie, dass Sie Simplepush Skript ändern müssen, nur ssl://gateway.sandbox.push.apple.com:2195 in Zeile 20 durch ssl://gateway.push.apple.com:2195 ersetzen und versuchen Sie es erneut.

Ich hoffe, es funktioniert für Sie.

+0

Zeit sparen, danke @Shongsu. – Rivendell