Local notification sound not working

半城伤御伤魂 提交于 2019-12-03 20:31:53

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
}
anjani

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