2017-05-31 35 views
1

私のアプリがバックグラウンドモードで殺すと音が鳴ります。このため私はVOIP通知を使用しています。私の方法は、フォアグラウンドモードでコールとサウンドを再生する方法です。しかし、バックグラウンドモードのメソッド呼び出し音は発火しません。ここに私のコードスニペットがあります。VOIPプッシュ通知

func pushRegistry(registry: PKPushRegistry, didReceiveIncomingPushWithPayload payload: PKPushPayload, forType type: String) { 
    var a = payload.dictionaryPayload 

     // NSBundle.mainBundle().pathForResource(<#T##name: String?##String?#>, ofType: <#T##String?#>) 

     let url = NSBundle.mainBundle().URLForResource("demo", withExtension: "mp3")! 

     do { 
      player = try AVAudioPlayer(contentsOfURL: url) 
      guard let player = player else { return } 

      player.prepareToPlay() 
      player.play() 
     } catch let error as NSError { 
      print(error.description) 
     } 
} 
+0

サウンドを再生しようとする前に、たぶん、あなたはAVAudioSessionを設定する必要がありますか?プロジェクトの機能を設定してください(ターゲット - >機能 - >バックグラウンドモード) –

+0

@AntonBelousovフォアグラウンドモードで正常に動作します。バックグラウンドモード用に別のコードを実行できますか? –

答えて

0

プッシュキットペイロードを受け取った後。サウンドファイルを使用してローカル通知をスケジュールする必要があります。サウンドファイルは最大30秒間再生されます。

import UIKit 
import PushKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate { 

    var window: UIWindow? 


    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 


    } 

} 

Refer

+0

あなたのために機能しましたか?はいの場合は、回答を受け入れることができます。 – Hasya