gpt4 book ai didi

ios - 为什么我的通知在 SWIFT3 中不起作用?

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:06 24 4
gpt4 key购买 nike

我使用 UIDatePicker 设置通知。当我在手机上运行该应用程序时,通知不起作用。

有什么问题吗?

我的代码:

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)
}
}

最佳答案

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:

  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 的委托(delegate)(例如在 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
    })

关于ios - 为什么我的通知在 SWIFT3 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39819337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com