【问题标题】:VOIP push notificationVOIP 推送通知
【发布时间】:2017-05-31 14:05:36
【问题描述】:

我想在我的应用程序在后台模式下终止时播放声音。为此,我正在使用 VOIP 通知。我的方法是前台模式下的通话和声音播放。但是在后台模式下,方法调用声音不会触发。这是我的代码 sn-p。

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

【问题讨论】:

  • 也许您应该在尝试播放声音之前配置 AVAudioSession?并配置你项目的能力(Target -> Capabilities -> Background Mode)
  • @AntonBelousov 它在前台模式下工作正常我们可以为后台模式做单独的代码

标签: ios swift voip


【解决方案1】:

一旦您收到 pushkit 有效负载。您必须使用声音文件安排本地通知。您的声音文件最多播放 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

【讨论】:

  • 它对你有用吗?如果是,那么您可以接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2023-03-26
  • 2020-09-03
  • 2018-05-06
  • 2017-01-20
相关资源
最近更新 更多