2014-11-04 80 views
2

我試圖製作鬧鐘應用程序,但是當我嘗試進行本地通知時,我無法播放聲音。我得到這個錯誤:本地通知聲音無法正常工作

[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 

} 
+0

的'localnotification'爲*功能的使用*名稱是有點混亂... – Honey 2017-02-13 16:56:29

回答

10

您需要徵得他的許可用戶發送本地通知在iOS的8

你可以這樣做當您的應用程序在您的AppDelegate方法中啓動時

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

感謝您的幫助,現在我得到通知但不播放聲音? – anjani 2014-11-04 12:06:48

+0

這是你有習慣的聲音嗎? – sbarow 2014-11-04 12:07:22

+3

我的建議是設置'localNotification.soundName = UILocalNotificationDefaultSoundName',看看聲音是否有效。如果確實如此,那麼你知道這是你的自定義聲音。 – sbarow 2014-11-04 12:09:04

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 
}