0

I've implemented Firebase Cloud Messaging as per instructions given on their website. So when I generate notification from here: enter image description here

And in AppDelegate.swift I've added didReceiveRemoteNotification function as:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],

     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        Messaging.messaging().appDidReceiveMessage(userInfo)
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

Other methods I've implemented are
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String)
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

Whereas my didFinishLaunchingWithOptions has:

 if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
            // For iOS 10 data message (sent via FCM
            Messaging.messaging().delegate = self
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

        FirebaseApp.configure()

        NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)

        return true
Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116

1 Answers1

1

Please take a look this gist file AppDelegate file for FCM

In XCode Goto Targets > Capabilities & make sure your Push Notifications is on. enter image description here Hope it helps.

Riajur Rahman
  • 1,976
  • 19
  • 28