我想播放一首歌。收到通知时。当应用程序为前台模式时,我的代码工作正常。我想在应用程序处于后台模式时播放歌曲。请帮忙。
最佳答案
向您的本地通知添加自定义声音
斯威夫特3.0
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
var state: UIApplicationState = application.applicationState
if state == .active {
var soundID: SystemSoundID
var mainBundle: CFBundleRef = CFBundleGetMainBundle()
var ref: CFURLRef? = CFBundleCopyResourceURL(mainBundle, ("mySoundName.wav" as? CFString), nil, nil)
AudioServicesCreateSystemSoundID(ref, soundID)
AudioServicesPlaySystemSound(soundID)
}
else {
// Push Notification received in the background
}
}
对于推送通知
将此添加到有效载荷。
{
aps =
{
alert = "message";
sound = "pushSound.caf";//this file will have to your bundle
};
}
关于swift - 在通知中播放歌曲,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43224858/