首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用终止时未调用UNUserNotificationCenter didReceive响应

应用终止时未调用UNUserNotificationCenter didReceive响应
EN

Stack Overflow用户
提问于 2017-02-05 17:13:34
回答 1查看 7.1K关注 0票数 7

我正在处理本地通知,但我遇到的问题是,当应用程序终止时,方法didReceive响应不会被调用,所以当我点击通知操作时,它只会启动应用程序,而不做任何其他事情。但是,当应用程序只在后台运行时,一切都照常运行。我的代码有什么问题吗?

//MyClassNameViewController.swift

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()

    UNUserNotificationCenter.current().delegate = self

}

func triggerAlarm1() {
    // Create an instance of notification center
    let center = UNUserNotificationCenter.current()

    // Sets the details of the notification
    let content = UNMutableNotificationContent()
    content.title = "Recorded Today's first alarm."
    content.body = "Be completely honest: how is your day so far?"
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "notificationID1"

    // Set the notification to trigger everyday
    let triggerDaily = Calendar.current.dateComponents([.hour,.minute], from: myTimePicker1.date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)

    // Deliver the notification
    let identifier = "UYLLocalNotification"
    let request = UNNotificationRequest(identifier: identifier,
                                        content: content, trigger: trigger)
    center.add(request, withCompletionHandler: { (error) in
        if error != nil {
            // Just in case something went wrong
            print(error!)
        }
    })

}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("didReceive Method called")

    if response.actionIdentifier == "actionOne" {
        let alertOne = UIAlertController(title: "First", message: "Some Message Here", preferredStyle: UIAlertControllerStyle.alert)
        let actionOne = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
        alertOne.addAction(actionOne)
        self.present(alertOne, animated: true, completion: nil)
    }
    completionHandler()
}

//AppDelegate.swift

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UNUserNotificationCenter.current().delegate = self

    // Request Authorisation
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert , .sound , .badge]) { (Bool, error) in
        // insert code here
    }

    let actionOne = UNNotificationAction(identifier: "actionOne", title: "Open1", options: [.foreground])
    let catogeryOne = UNNotificationCategory(identifier: "notificationID1", actions: [actionOne], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([catogeryOne])

    return true
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-06 00:25:59

在你的动作标识符内调用这个函数,你就会好起来!

代码语言:javascript
复制
 func alertAction() {

    let alertController = UIAlertController(title: "Hello", message: "This is cool!", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
        // Do something with handler block
    }))

    let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
    let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]

    presentedViewController.present(alertController, animated: true, completion: nil)
}

这非常简单!

代码语言:javascript
复制
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("didReceive Method called")

    if response.actionIdentifier == "actionOne" {
        DispatchQueue.main.async(execute: {
            self.alertAction()
        })
    } else if response.actionIdentifier == "actionTwo" {

    } else if response.actionIdentifier == "actionThree" {

    }
    completionHandler()
}

完全适用于Swift 3.0和Xcode 8.0。我已经更改了视图控制器之间的所有连接。我已经在初始ViewController中添加了一个NavigationController

AppDelegate:

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    let center = UNUserNotificationCenter.current()
    center.delegate = self

    // Request Authorisation
    center.requestAuthorization(options: [.alert , .sound , .badge]) { (Bool, error) in
        // insert code here
    }

    let actionOne = UNNotificationAction(identifier: "actionOne", title: "Open1", options: [.foreground])
    let catogeryOne = UNNotificationCategory(identifier: "notificationID1", actions: [actionOne], intentIdentifiers: [], options: [])

    let actionTwo = UNNotificationAction(identifier: "actionTwo", title: "Open2", options: [.foreground])
    let catogeryTwo = UNNotificationCategory(identifier: "notificationID2", actions: [actionTwo], intentIdentifiers: [], options: [])

    let actionThree = UNNotificationAction(identifier: "actionThree", title: "Open3", options: [.foreground])
    let catogeryThree = UNNotificationCategory(identifier: "notificationID3", actions: [actionThree], intentIdentifiers: [], options: [])

    UNUserNotificationCenter.current().setNotificationCategories([catogeryOne, catogeryTwo, catogeryThree])

    return true
}


func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("willPresent method called")
    completionHandler([.alert, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("didReceive Method called")

    if response.actionIdentifier == "actionOne" {
        DispatchQueue.main.async(execute: {
            self.alertAction()
        })
    } else if response.actionIdentifier == "actionTwo" {

    } else if response.actionIdentifier == "actionThree" {

    }
    completionHandler()
}



func alertAction() {

    let alertController = UIAlertController(title: "Hello", message: "This is cool!", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
        // Do something with handler block
    }))

    let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
    let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]

    presentedViewController.present(alertController, animated: true, completion: nil)
}

此外,我已经从viewDidLoad和其他地方删除了所有以前的建议。

更改与show的连接,并且不要以模式显示。如果你想在任何地方显示你的提醒。祝好运

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42050324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档