【问题标题】:Custom Sound not playing for remote notification on iOS自定义声音未在 iOS 上播放远程通知
【发布时间】:2017-05-02 15:28:47
【问题描述】:

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

我的通知负载如下所示。

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

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

我有在其他情况下播放声音文件的代码,所以我注意到声音文件很好。它的长度为 29 秒,因此少于 Apple 规定的 30 秒。

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

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

我在两种不同的设备上进行了测试,一部手机和一部 ipad,但都没有成功。它们都运行 iOS 10.3.1。

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

【问题讨论】:

    标签: ios iphone notifications apple-push-notifications


    【解决方案1】:

    请完成以下问题。这可能对您有所帮助。

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

    我能够使用上述调用为本地通知播放自定义声音。

    这可能对你有帮助。

    【讨论】:

    • 嗯,我收到的是远程通知,而不是创建本地通知。但是:“请勿打扰”模式已关闭。声音文件格式为 caf,长度为 29 秒。并添加到主包中。声音已打开。
    • 不,问题没有解决,我还是没有声音。
    • 你能用代码更新问题吗?所以我们可以有更清晰的想法。请查看更新的答案,这可能会有所帮助。
    • 你想要什么代码?远程通知应该在应用程序关闭时播放声音(不仅仅是在后台,而是完全关闭)。所以我在应用程序中拥有的唯一代码(与此相关)实际上是首先注册推送通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2019-04-11
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多