如何播放Firebase推送通知iOS的自定义声音?(Swift)

如何播放Firebase推送通知iOS的自定义声音?(Swift),ios,firebase,swift3,firebase-cloud-messaging,appdelegate,Ios,Firebase,Swift3,Firebase Cloud Messaging,Appdelegate,我用的是swift 3。我在我的应用程序中使用Firebase推送通知。 当前im获取推送通知事件应用程序位于前台、后台或关闭应用程序。 在那里,我配置了一个定制的通知声音,当推送通知到来时。但当应用程序关闭或屏幕锁定时,它无法听到声音。但是通知已经出现了 以下是我迄今为止所做的工作: import UIKit import UserNotifications import Firebase import FirebaseInstanceID import FirebaseMessaging i

我用的是swift 3。我在我的应用程序中使用Firebase推送通知。 当前im获取推送通知事件应用程序位于前台、后台或关闭应用程序。 在那里,我配置了一个定制的通知声音,当推送通知到来时。但当应用程序关闭或屏幕锁定时,它无法听到声音。但是通知已经出现了

以下是我迄今为止所做的工作:

import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    //For FireBase Configs
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()


    FirebaseApp.configure()

    return true
}



func application(_ application: UIApplication,open url: URL,sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application,open: url as URL!,sourceApplication: sourceApplication,annotation: annotation)
}



//FireBase Notifications Begins
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {


    //Play alert sound
    utilityHelper.playAudio(fileName: "udio", fileExtenstion: "mp3")

    let json = JSON(userInfo)

    //show notification

    completionHandler(UIBackgroundFetchResult.newData)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    NSLog("show notification")
    completionHandler(UNNotificationPresentationOptions.alert)
}


func application(received remoteMessage: MessagingRemoteMessage) {


}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {



}


func application(_ application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken as Data

    if let regToken = InstanceID.instanceID().token() {


    }
}
//FireBase Notifications End

}

如果我从firebase启用声音,它也会给我一个默认声音,而不是自定义声音。应用程序关闭时如何播放声音?

您需要将声音文件转换为.m4a文件这与我认为的格式无关,而是在应用程序在前台运行时播放声音。当应用程序关闭时,它不会播放。请参考此答案