本地通知声音不起作用
Posted
技术标签:
【中文标题】本地通知声音不起作用【英文标题】:Local notification sound not working 【发布时间】:2014-11-04 11:45:27 【问题描述】:我正在尝试制作闹钟应用,但是当我尝试制作本地通知时,我无法播放声音。我收到了这个错误:
[user info = (null) with a sound but haven't received permission from the user to play sounds]
代码如下:
@IBOutlet var setAlaram: UIDatePicker!
@IBAction func setAlarmButton(sender: AnyObject)
var dateformater = NSDateFormatter()
dateformater.timeZone = NSTimeZone .defaultTimeZone()
dateformater.timeStyle = NSDateFormatterStyle.ShortStyle
dateformater.dateStyle = NSDateFormatterStyle.ShortStyle
var datetimestring = NSString()
datetimestring = dateformater.stringFromDate(setAlaram.date)
println(datetimestring)
[self .localnotification(setAlaram.date)]
func localnotification (firedate:NSDate)
var localNotification:UILocalNotification = UILocalNotification()
localNotification.fireDate = firedate
localNotification.alertBody = "time to woke up"
localNotification.soundName = "alarm.wav"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
override func viewDidLoad()
super.viewDidLoad()
let date1 = NSDate()
setAlaram.date = date1
【问题讨论】:
将localnotification
用作函数名称有点令人困惑...
【参考方案1】:
您需要在 ios 8 中询问用户发送本地通知的权限。
您可以在 AppDelegate 方法中启动应用程序时这样做
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
return true
【讨论】:
感谢您的帮助,现在我收到通知但没有播放声音? 你有自定义的声音吗? 我的建议设置为localNotification.soundName = UILocalNotificationDefaultSoundName
,看看声音是否有效。如果是,那么您知道这是您的自定义声音。
我找到了解决方案,它在真实设备上而不是在模拟器上运行,我仍然可以使用 localNotification.soundName = "alarm.wav" 但感谢您尝试帮助我:)【参考方案2】:
在 sbarow 的帮助下发布最终答案我发现这里的解决方案是可能对其他人有帮助的代码。
1.AppDelegate.swift 替换这个:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
return true
2.ViewController.swift:
@IBOutlet var setAlaram: UIDatePicker!
@IBAction func setAlarmButton(sender: AnyObject)
var dateformater = NSDateFormatter()
dateformater.timeZone = NSTimeZone .defaultTimeZone()
dateformater.timeStyle = NSDateFormatterStyle.ShortStyle
dateformater.dateStyle = NSDateFormatterStyle.ShortStyle
var datetimestring = NSString()
datetimestring = dateformater.stringFromDate(setAlaram.date)
println(datetimestring)
[self .localnotification(setAlaram.date)]
func localnotification (firedate:NSDate)
var localNotification:UILocalNotification = UILocalNotification()
localNotification.fireDate = firedate
localNotification.alertBody = "time to woke up"
localNotification.soundName = "alarm.wav"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
override func viewDidLoad()
super.viewDidLoad()
let date1 = NSDate()
setAlaram.date = date1
【讨论】:
以上是关于本地通知声音不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Ionic 3 本地通知 LED/声音在 Android 中不起作用