2016-10-02 5 views
1

私はUIDatePickerを使って通知をセットアップしました。私の電話でアプリケーションを実行すると、通知が機能していません。SWIFT3で通知が機能しないのはなぜですか?

問題が何ですか?

マイコード:AppDelegate.swift

import UIKit 
import UserNotifications 

class ViewController: UIViewController { 
    @IBOutlet weak var datePicker: UIDatePicker! 

    var timer = Timer()  
    var time = 10 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     scheduleLocalNotification() 
    } 


    func scheduleLocalNotification() { 
     let notification = UILocalNotification() 
     let dateTime = datePicker.date 
     notification.alertAction = "Call" 
     notification.alertBody = "You have a call right now" 
     notification.fireDate = dateTime 
     notification.timeZone = NSTimeZone.default 
     UIApplication.shared.scheduleLocalNotification(notification) 
    } 


    @IBAction func pushNotification(_ sender: AnyObject) { 
     let AlertView = UIAlertController(title: "Time for your call!", message: "Press go to continue", preferredStyle: UIAlertControllerStyle.alert) 
     AlertView.addAction(UIAlertAction(title: "Go", style: UIAlertActionStyle.default, handler: nil)) 
     self.present(AlertView, animated: true, completion: nil)  
    } 
} 
+0

あなたの 'pushNotification'アクションはどのようなトリガーですか? –

+0

あなたに適したソリューションを見つけましたか? –

答えて

2

のiOS 8 - iOS9:

  1. @FrederikFrandsenは、あなたが最初のユーザーの通知用にアプリケーションを登録する必要があり、言ったように:

    let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)   
    UIApplication.shared.registerUserNotificationSettings(settings) 
    
  2. その後、あなたは次のUIApplicationDelegate機能を実装することにより、あなたのAppDelegate内のローカル通知を処理することができます

    func application(_ application: UIApplication, didReceive notification: UILocalNotification) { 
        // handle your local notification here 
    } 
    

iOS 10:

    あなたのAppDelegateで
  1. が新しいUNUserNotificationCenterDelegate追加:

    import UserNotifications 
    
    extension AppDelegate: UNUserNotificationCenterDelegate { 
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
         completionHandler([.alert, .badge, .sound]) 
        } 
    
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
         completionHandler() 
        } 
    } 
    
  2. UNUserNotificationCenterのデリゲートを設定します(たとえば、 )applicationDidFinishLaunchingに:

    UNUserNotificationCenter.current().delegate = self 
    
  3. リクエストの承認:

    let options: UNAuthorizationOptions = [.alert, .badge, .sound] 
    UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (success, error) in 
        // handle error 
    }) 
    
  4. トリガー、ユーザー通知:iOSの10で

    let components = NSCalendar.current.dateComponents([.hour, .minute], from: datePicker.date) 
    
    let content = UNMutableNotificationContent() 
    content.title = "title" 
    content.body = "body" 
    content.sound = UNNotificationSound.default() 
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false) 
    let request = UNNotificationRequest(identifier: "myRequest", content: content, trigger: trigger) 
    
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in 
        // handle error 
    }) 
    
+0

「ここのローカル通知を処理する」とはどういう意味ですか?そこに通知をどのように表示できますか?スケジューリングでは無限ループが発生します。 –

+0

@NumanKaraaslanこれは、アプリケーションがiOS 9まで「UILocalNotification」を受け取ったときに呼び出される関数の本体です。あなたはその場所から通知をスケジュールしません。その時点で通知をスケジュールできます。 iOS 10以降、UNUserNotificationCenterDelegateを使用して通知をアプリケーションに表示することができます。 iOS 10の前に、カスタムUIを使用して、アプリに既に入っているときに通知を表示する必要があります。 –

0

あなたがここにこれが必要:

let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 

UIApplication.shared.registerUserNotificationSettings(settings) 
UIApplication.shared.registerForRemoteNotifications() 
+0

'UIApplication.shared.registerForRemoteNotifications()'はローカル通知には必要ありませんが、そうです。それはうまくいくはずです。 @krishあなたのアプリが開いているときに通知が表示されないことにご注意ください。 –

1

は、UILocalNotificationが推奨されていません。あなたはそれを仕事の財産にすることはできません。新しいユーザー通知フレームワークを使用するようにコードを移行する必要があります。

+0

iOS 10以降のデプロイメントターゲットを使用している場合、これは単なる問題ではありませんか?私はDeployment Targetバージョンでサポートされている推奨されなくなったコードに問題はありませんでした。 –

関連する問題