Ios 本地通知声音不起作用
我有一个应用程序,可以发出带有声音的本地通知 但当通知触发时,如果我的应用程序正在运行,它不会播放声音。只有当我关闭应用程序时,它才会播放通知声音 我希望它能够播放声音,即使应用程序正在手机上运行 这是我的密码:Ios 本地通知声音不起作用,ios,swift,notifications,Ios,Swift,Notifications,我有一个应用程序,可以发出带有声音的本地通知 但当通知触发时,如果我的应用程序正在运行,它不会播放声音。只有当我关闭应用程序时,它才会播放通知声音 我希望它能够播放声音,即使应用程序正在手机上运行 这是我的密码: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // register
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// register for sending notifications
let notificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
application.registerUserNotificationSettings(settings)
return true
}
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = "Local notifications are working"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
有什么帮助吗?您必须使用didReceiveLocalNotification手动播放声音
我可以在这里播放默认的通知声音吗?您不能@mikle94您必须添加自定义声音
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
print("received local notification")
application.applicationIconBadgeNumber = 0
//Play sound
let soundURL = NSBundle.mainBundle().URLForResource("beep", withExtension: "wav")
var mySound: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL, &mySound)
AudioServicesPlaySystemSound(mySound)
var alert = UIAlertView()
alert.title = "Alert"
alert.message = notification.alertBody
alert.addButtonWithTitle("Dismiss")
alert.show()
}