ios - VOIP 推送通知

标签 ios swift 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)
        }
}

最佳答案

一旦您收到 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

关于ios - VOIP 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44286984/

相关文章:

ios - 将数据从一个类传递到另一个类

ios - 转换任何对象?至 NSD 日期

ios - 如何将核心数据关系设置为 NSNull()?

ios - Tableview 第一部分弹回

c# - 是否有任何 C# 库包装 Xmpp Jingle 或包装用另一种语言编写的 Xmpp Jingle 库

ios - 如何将背景图像设置为标签

iphone - OCMock的andDo访问参数时出现EXC_BAD_ACCESS

swift - 什么时候只为模拟器构建一个框架有用?还是仅限设备?

ios - VoIP 推送通知在装有 iOS 10.3 的 iPhone 7 上崩溃

voip - 使用 Asterisk Sip 拨出 - 线路繁忙错误