Local notification sound not working

若如初见. 提交于 2019-12-21 06:18:34

问题


I'm trying to make an alarm app but when I try to make a local notification I am unable to play the sound. I got this error:

[user info = (null)} with a sound but haven't received permission from the user to play sounds]

Here is the code:

@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

}

回答1:


You need to ask the user for permission to send local notifications in iOS 8.

You can do so when you application launches in your AppDelegate methods

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
    return true
}



回答2:


Posting the final answer with the help of sbarow i found the solution here is the code it may be helpful for others.

1.AppDelegate.swift replace this:

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
}


来源:https://stackoverflow.com/questions/26734350/local-notification-sound-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!