2016-04-18 91 views
4

this articleM Penades' answer VoIP通知デモをフォローしています。 didRegisterUserNotificationSettingsを使用して通知を登録でき、didUpdatePushCredentialsからvoipトークンを取得できます。PushKitを使用してVoIP通知を受信できません

しかし、houstonを使用してデバイスに通知を送信すると、コンソールからは1 push notification sent successfullyメッセージを受け取ることはできますが、デバイス側にはアクションやログは記録されません。 didReceiveIncomingPushWithPayloadが呼び出されていないようです。

P.S.私は開発のためにXcode 7.3、iPhone 6(iOS 9.3)、iPad Mini(iOS 9.3)を使用しています。インストールのための分散アドホック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で通知を送信しようとしたときにエラーメッセージが表示されましたが、houstonコンソールラインツールでエラーが表示されないことがあります。また、正しい識別子と証明書を使用しているかどうかも確認してください。 – ujell

+0

あなたの端末で最終的に通知を受けましたか?上に掲載したネイティブの迅速なコードを変更する必要がありますか?ありがとう。 – Shongsu

+0

私の場合、間違った.pemファイルで通知を送信しようとしていたので、修正されました。また、デバイストークンは開発と配布のために異なっていることに注意してください。これはあなたのケースでも問題になるかもしれません。私が何かを見逃していなければ、コードは大丈夫です。 – ujell

答えて

6

最後にこの問題を修正しました。 素早いソースコードは問題ありません。問題は私がAppleの開発サーバーgateway.sandbox.push.apple.comにプッシュ要求を誤って送っていることです。実際にはappleのプロダクションサーバーgateway.push.apple.comにリクエストを送信する必要があります。

あなたがM Penades' procedureを以下している場合は、単にライン20でssl://gateway.push.apple.com:2195ssl://gateway.sandbox.push.apple.com:2195を交換して再度試して、あなたがSimplepushスクリプトを変更する必要があることに注意してください。

ご希望の場合はご利用ください。

+0

保存時間、ありがとう@Shongsu。 – Rivendell

関連する問題