2016-10-02 57 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或更高版本的部署目标时,这不仅仅是一个问题吗?我从未遇到部署目标版本支持的弃用代码问题。 –

相关问题