1

I have the following appDelegate:

import UIKit
import CoreData
import UserNotifications

@available(iOS 11.0, *)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
    var window: UIWindow?

    ...

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
    {
        print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }
}

I am using ios11 and swift 4.1 but the notification gets fired off correctly. But when I tap it I can't grab it in the app. Why? I am using xcode 9.4.1. Thanks.

The app is in the background and when the notification is tapped the app opens up.

cdub
  • 24,555
  • 57
  • 174
  • 303
  • what's the state of app? background/closed/opened? – Lal Krishna Aug 03 '18 at 06:07
  • What is local push notification ? is it push notification or local notification ? – vivekDas Aug 03 '18 at 06:10
  • Updated the question – cdub Aug 03 '18 at 06:22
  • If you just want to get local notification response, try it userNotificationCenter. I think your code is for remote notification (didReceive**Remote**Notification) – Phineas Huang Aug 03 '18 at 06:36
  • I want to grab the local notification when someone taps on it. How is that done? I have these answers: https://stackoverflow.com/questions/51626479/open-a-view-controller-from-a-local-push-notification-in-ios-11/51665847?noredirect=1#comment90295635_51665847 – cdub Aug 03 '18 at 06:41
  • @cdub it wont call didReceiveRemoteNotification method because you are not getting notification from remote server so you should use UNUserNotificationCenter for this. Please refer below answer. – Sandip Gill Aug 03 '18 at 06:45
  • Yeah I got it now. – cdub Aug 03 '18 at 06:53

1 Answers1

1

Hello @cdub You should use usernotificatioCenter

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    // do code here to show notification alert in for-ground mode 
}




func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //your notification handling code here 
    }
Sandip Gill
  • 1,060
  • 1
  • 11
  • 22