自定义声音无法在iOS上播放远程通知

时间:2017-05-02 15:28:48

标签: ios iphone notifications apple-push-notifications

在我尝试更改声音文件后,我的通知上的自定义声音最近停止了为我的应用程序工作。

我的通知有效负载如下所示。

{
"aps" : {
        "alert" : "Your message here.",
        "badge" : 0,
        "sound" : "notification.caf",
        "content-available" : 1
    }
}

我的应用程序包中有notification.caf。

我有在其他情况下播放声音文件的代码,所以我注意到声音文件很好。这是29秒长,所以不到Apple规定的30个。

在设置 - >通知下,应用程序下的所有内容都已启用。当应用程序设置为横幅和警报时,我已经尝试过测试。

但是每当我关闭我的应用程序并从我的网络服务发送通知时,手机就会振动并显示通知。没有默认声音,没有自定义声音。

我已经在两种不同的设备上测试过,一部手机和一部ipad,但都没有工作过。它们都运行iOS 10.3.1。

有没有人知道出了什么问题?

1 个答案:

答案 0 :(得分:0)

请仔细阅读以下问题。这对你有帮助。

UILocalNotification custom sound is not playing in iOS7

您可以尝试的另一件事是将.caf或.mp3文件添加到ImageAsset并尝试。

出于某种原因,在我的一个应用程序中,我无法播放自定义声音。但是在我将.mp3文件添加到资产文件夹后,我能够播放自定义声音进行远程通知。

请尝试以下代码。

import UIKit
import Foundation
import AudioToolbox
import AVFoundation

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, AVAudioPlayerDelegate, AlarmApplicationDelegate{

    var window: UIWindow?
    var audioPlayer: AVAudioPlayer?
    let alarmScheduler: AlarmSchedulerDelegate = Scheduler()
    var alarmModel: Alarms = Alarms()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        var error: NSError?
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch let error1 as NSError{
            error = error1
            print("could not set session. err:\(error!.localizedDescription)")
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch let error1 as NSError{
            error = error1
            print("could not active session. err:\(error!.localizedDescription)")
        }
        window?.tintColor = UIColor.red

        return true
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        //show an alert window
        let storageController = UIAlertController(title: "Alarm", message: nil, preferredStyle: .alert)
        var isSnooze: Bool = false
        var soundName: String = ""
        var index: Int = -1
        if let userInfo = notification.userInfo {
            soundName = userInfo["sound"] as! String
        }

        playSound(soundName)

    }

    //print out all registed NSNotification for debug
    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {

        print(notificationSettings.types.rawValue)
    }

    func playSound(_ soundName: String) {

        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        AudioServicesAddSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate),nil,
                                              nil,
                                              { (_:SystemSoundID, _:UnsafeMutableRawPointer?) -> Void in
                                                AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
            },
                                              nil)
        let url = URL(fileURLWithPath: Bundle.main.path(forResource: soundName, ofType: "mp3")!)

        var error: NSError?

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: url)
        } catch let error1 as NSError {
            error = error1
            audioPlayer = nil
        }

        if let err = error {
            print("audioPlayer error \(err.localizedDescription)")
            return
        } else {
            audioPlayer!.delegate = self
            audioPlayer!.prepareToPlay()
        }

        //negative number means loop infinity
        audioPlayer!.numberOfLoops = -1
        audioPlayer!.play()
    }

}

我可以使用上述通话播放自定义声音以进行本地通知。

这可能对您有所帮助。

相关问题