2016-04-18 125 views
4

我正在關注this articleM Penades' answer構建VoIP通知演示。我可以使用didRegisterUserNotificationSettings註冊通知,並且可以從didUpdatePushCredentials獲得voip令牌。無法使用PushKit接收VoIP通知

但是,當我使用休斯敦模擬發送通知到我的設備,我可以從控制檯得到1 push notification sent successfully消息,但沒有操作或設備端的日誌。看起來didReceiveIncomingPushWithPayload從未被調用過。

P.S.我正在使用Xcode 7.3,iPhone 6(iOS 9.3)和iPad Mini(iOS 9.3)進行開發。分佈式Ad Hoc ipa進行安裝。

此處張貼的代碼。

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" 
     } 
    } 
} 
} 

關於如何接收此類通知的任何想法?

+1

我有同樣的問題,當我試圖發送與node-apn的通知我得到了一個錯誤消息,似乎休斯敦控制檯線工具不顯示錯誤的一些情況。另外請確保您使用的是正確的標識符和證書。 – ujell

+0

您是否最終在設備上收到了您的通知?我需要對上面發佈的本機swift代碼進行任何更改嗎?謝謝。 – Shongsu

+0

在我的情況下,我試圖發送一個錯誤的.pem文件的通知,所以是的,這已被修復。另外請注意,設備令牌在開發和分發方面有所不同,這可能也是您的情況中的一個問題。代碼看起來不錯,如果我沒有錯過任何東西。 – ujell

回答

6

最後我解決了這個問題。 快速源代碼有問題,問題是我發送推送請求到蘋果的開發服務器gateway.sandbox.push.apple.com,實際上我們應該發送請求到蘋果的生產服務器gateway.push.apple.com

如果你正在跟蹤M Penades' procedure,請注意,您需要修改Simplepush腳本,只需通過ssl://gateway.push.apple.com:2195在第20行替換ssl://gateway.sandbox.push.apple.com:2195,然後再試一次。

希望它適合你。

+0

節省時間,謝謝@Shongsu。 – Rivendell