2016-10-02 47 views
1

我使用UIDatePicker設置通知。當我在手機上運行應用程序時,通知不起作用。爲什麼我的通知不能在SWIFT3中工作?

有什麼問題?

我的代碼:

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. 然後你就可以在你的AppDelegate處理本地通知通過實施以下UIApplicationDelegate功能:

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

iOS 10:

  1. 在AppDelegate中添加新的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. 觸發您的用戶通知:

    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之前,您必須使用自定義用戶界面才能在您已經在應用中時顯示通知。 –

0

在AppDelegate.swift

你這需要在這裏:

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

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

'UIApplication.shared.registerForRemoteNotifications()'不需要本地通知 - 但是。這應該工作。 @krish請注意,當您的應用程序處於打開狀態時,您看不到通知。 –

1

在iOS系統10,UILocalNotification已被棄用。你將無法得到它的工作財產。您需要遷移您的代碼以使用新的用戶通知框架。

+0

使用iOS 10或更高版本的部署目標時,這不僅僅是一個問題嗎?我從未遇到部署目標版本支持的棄用代碼問題。 –

相關問題