简体   繁体   English

本地通知声音不起作用

[英]local notification sound doesn't work

I have an application that fires a local notification with sound. 我有一个可以发出声音的本地通知的应用程序。

But when notification fires, it doesn't play sound if my app is running. 但是,当触发通知时,如果我的应用程序正在运行,则不会播放声音。 Only when i close my application it plays a notification sound. 只有当我关闭我的应用程序时,它才会播放通知声音。

I want it to play sound even if application is running on phone. 我希望它即使在手机上运行应用程序也能播放声音。

Here is my code: 这是我的代码:

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)

Any help? 有什么帮助吗?

You have to manually play sound with didReceiveLocalNotification 您必须使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM