Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS上不播放远程通知的自定义声音_Ios_Iphone_Notifications_Apple Push Notifications - Fatal编程技术网

在iOS上不播放远程通知的自定义声音

在iOS上不播放远程通知的自定义声音,ios,iphone,notifications,apple-push-notifications,Ios,Iphone,Notifications,Apple Push Notifications,在我尝试更改声音文件后,我的通知中的自定义声音最近已停止在我的应用程序中工作 我的通知负载看起来像这样 { "aps" : { "alert" : "Your message here.", "badge" : 0, "sound" : "notification.caf", "content-available" : 1 } } 我的应用程序包中有notification.caf 我有在其他情况下播放声音文件的代码,所以

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

我的通知负载看起来像这样

{
"aps" : {
        "alert" : "Your message here.",
        "badge" : 0,
        "sound" : "notification.caf",
        "content-available" : 1
    }
}
我的应用程序包中有notification.caf

我有在其他情况下播放声音文件的代码,所以我注意到声音文件很好。它有29秒长,比苹果规定的30秒还短

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

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

我在两种不同的设备上进行了测试,一台手机和一台ipad,但都不起作用。它们都运行iOS 10.3.1


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

请仔细回答下面的问题。这对你有帮助

您可以尝试的另一件事是将.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秒。并添加到主捆绑包中。和声音。不,问题没有解决,我仍然没有收到声音。你能用代码更新问题吗?所以我们可以有更清楚的想法。请参阅更新的答案,这可能会有帮助。你想要什么代码?远程通知应该在应用程序关闭时播放声音(不仅在后台,而且完全关闭)。因此,我在应用程序中唯一的代码(与此相关)实际上是首先注册推送通知。