[英]local notification sound doesn't work
我有一個可以發出聲音的本地通知的應用程序。
但是,當觸發通知時,如果我的應用程序正在運行,則不會播放聲音。 只有當我關閉我的應用程序時,它才會播放通知聲音。
我希望它即使在手機上運行應用程序也能播放聲音。
這是我的代碼:
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
手動播放聲音
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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.