2015-08-28 66 views
0

正在嘗試一個本地通知演示,並在我的iPhone徽章&警報通知工作除了sound.Here好是我的代碼,的iOS 8:聲音設置不起作用的UIUserNotificationType

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 

      let notificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound 
      let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil) 
      application.registerUserNotificationSettings(settings) 

      application.applicationIconBadgeNumber = 0 // resetting the badge number to again 0 

      return true 
     } 

     func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 

      application.applicationIconBadgeNumber = 0 //resetting the badge number to again 0 

     } 

這裏是mainviewcontroller代碼片段

class ViewController: UIViewController { 


    @IBAction func startNotification(sender: UIButton) { 

     var localNotification = UILocalNotification() 
     localNotification.fireDate = NSDate(timeIntervalSinceNow: 5) // notification will be sent after 5 seconds from clicking 
     localNotification.alertBody = "Notification came" 
     localNotification.timeZone = NSTimeZone.defaultTimeZone() // Time zone of the notfication's fire date 
     localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 //The number to diplay on the icon badge. We will increment this number by one. 

     UIApplication.sharedApplication().scheduleLocalNotification(localNotification) // scheduling the notification 

    } 

注:即使我去設置 - > demoApp - >通知並啓用警報,徽章&聲音,即使在當時的聲音通知不存在的

任何幫助,將不勝感激。

回答

0

嘗試在模擬器上測試它嗎?如果它工作在模擬器,而不是在設備,確保靜音開關是不是

+0

我剛纔在模擬器嘗試....但沒有用途..它沒有工作:( –

0

最後我的問題得到有效解決,

1)該行必須在你的代碼,

localNotification.soundName = UILocalNotificationDefaultSoundName 

2)確保該DONOT打擾模式爲「關」(向上滑動從下你的電話,你會看到一個半月符號)

3)這個錯誤是我做了什麼 - >>手機在靜音模式(在你的iPhone側面會有一個開關附近的音量按鈕的)

4 )進入設置 - >通知 - >應用程序 - >啓用允許通知

5)進入設置 - >通知 - >應用程序 - >啓用聲音

6)通過在側面的音量按鈕增加振鈴音量..

希望這可以幫助其他人..... :)