我有一个用声音发出本地通知的应用程序。
但是当通知触发时,如果我的应用程序正在运行,它就不会播放声音。只有当我关闭我的应用程序时,它才会播放通知声音。
即使应用程序在手机上运行,我希望它能播放声音。
这是我的代码:
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)
任何帮助?
答案 0 :(得分:0)
您必须使用didReceiveLocalNotification
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()
}