0

So what i am trying to achieve is when a notification launch and the user tap on it, an Alert controller should appear with 2 options. However, when the app launch from the notification tap, nothing appears.

These codes are inside the AppDelegate.swift file

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

func showAlert() {
    let alert = UIAlertController(title: "Confirm", message: "Confirm?", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "No", style: .destructive, handler: nil))
    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
    window?.rootViewController?.present(alert, animated: true, completion: nil)
}
emrcftci
  • 3,355
  • 3
  • 21
  • 35
unique27
  • 1
  • 3

3 Answers3

1
var topVC: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
topVC?.rootViewController = UIViewController()
let alert = UIAlertController(title: "Alert", message: "Notification Received", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in
    // action OK
 })
topVC?.makeKeyAndVisible()
topVC?.rootViewController?.present(alert, animated: true, completion: nil)
Krupa Parsaniya
  • 287
  • 2
  • 13
  • Still nothing... Shows me this warning: Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called. – unique27 Nov 14 '19 at 05:50
  • 1
    @unique27Are you trying to tap on notification after clear app from device? – Krupa Parsaniya Nov 14 '19 at 09:51
  • Not exactly. My notifications can be tapped either when the app is running foreground and background. Trying both results in the same issue. No alerts. – unique27 Nov 15 '19 at 06:53
0

Try this:

func showAlert() {

var alertController = UIAlertController(title: "Confirm", message: "Confirm?", preferredStyle: UIAlertControllerStyle.alert)
var okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
                    UIAlertAction in
                    // action
                }
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
                    UIAlertAction in
                   // action
                }
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

}
Nick
  • 875
  • 6
  • 20
0

I think you are calling the func showAlert() from the wrong place. When the app launch from the notification tap, the app gets an event in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

So you should try this,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    //Handle remote notification event on app launch
    if let remoteNotification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] {
        showAlert()
    }
}
Nikunj
  • 630
  • 1
  • 6
  • 20
  • Unfortunately still didn't work... I read from somewhere that for iOS 13, the methods in AppDelegate will be ignored for notifications? Cause I tried another method to instantiate a view controller from notification tap and only devices under iOS 13 works correctly. – unique27 Nov 15 '19 at 01:08
  • Which notifications are you using? local or remote? – Nikunj Nov 15 '19 at 03:49
  • Local notifications – unique27 Nov 15 '19 at 06:52