2016-09-21 7 views
-2

私はローカルの通知とその正常な動作を作成しました。私が通知を見るためにクリックした場合、その曲は再生されていません、私はその曲がユーザーがそれを停止するまで再生することを望みます。 2.ユーザーが曲を停止できるボタンが1つ必要です。swiftを使用してローカル通知を作成する方法は?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
    self.generateLocalNotification() 
    return true 
} 

func generateLocalNotification(){ 

    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! 
    var dateFire=NSDate() 

    var fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate()) 

    if (fireComponents.hour >= 9) { 
     // dateFire=dateFire.dateByAddingTimeInterval(86400) // Use tomorrow's date 
     fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate()) 
    } 
    fireComponents.hour = 9 
    fireComponents.minute = 0 
    dateFire = calendar.dateFromComponents(fireComponents)! 
    let localNotification = UILocalNotification() 
    localNotification.fireDate = dateFire 
    localNotification.repeatInterval = NSCalendarUnit.Day 
    localNotification.timeZone = NSCalendar.currentCalendar().timeZone 
    localNotification.alertBody = " Started!" 
    localNotification.alertAction = "hear the song!" 
    localNotification.soundName = "jingle-bells-sms.m4r" 
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 

} 
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    if application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background { 

     let alertMessage = UIAlertController(title: "NAME", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert) 

     let image = UIImage(named: "1_flash") 

     let action = UIAlertAction(title: "STOP", style: UIAlertActionStyle.Cancel, handler:nil) 
     action.setValue(image, forKey: "image") 
alertMessage.addAction(action) 
     self.window?.rootViewController?.presentViewController(alertMessage, animated: true, completion: nil) 

    } 

    else 
    { 
} 
+0

私たちはあなたを助けることができるので、関連コードを共有する必要があります。 – Andrej

+0

サウンドファイルは30秒間だけ再生されます。リピートモードで30秒のサウンドファイルを再生するリピートローカル通知を設定する必要があります。 – Hasya

答えて

関連する問題