应用终止时未调用UNUserNotificationCenter didReceive响应 [英] UNUserNotificationCenter didReceive response not called when app is terminated

查看:99
本文介绍了应用终止时未调用UNUserNotificationCenter didReceive响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理本地通知,但是我遇到的问题是,应用终止时未调用didReceive Response方法,因此当我点击通知操作时,它只会启动该应用,而没有执行其他任何操作.但是,当应用程序仅在后台运行时,一切照常运行.我的代码有什么问题吗?

I'm working on local notifications but the problem I have is that the method didReceive Response is not being called when the app is terminated so when I tap on a notification action it just launches the app and did nothing else. But when the app is just in the background everything works as usual. Anything wrong with my code?

//MyClassNameViewController.swift

//MyClassNameViewController.swift

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

//AppDelegate.swift

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
}

推荐答案

在您的动作标识符中调用此函数,您会没事的!

Call this function inside of your action identifier and you'll be ok!

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

超级容易!

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.

Fully works on Swift 3.0 and Xcode 8.0. I have changed all of the connections between the view controller. I've added a NavigationController to the initial ViewController.

AppDelegate:

AppDelegate:

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和其他地方删除了所有先前的建议.

Also I have deleted all of the previous suggestions from the viewDidLoad and other places.

更改与show的连接,并且不显示为模态.如果您想在任何地方显示警报.祝你好运

Change your connections to the show and do not present as modally. If you want to show your alerts everywhere. Good luck

这篇关于应用终止时未调用UNUserNotificationCenter didReceive响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆