Search code examples
iosswiftapple-push-notificationsswift4ios11

Local notification not calling didReceiveRemoteNotifications in swift 4


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.


Solution

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