0

Notification starts coming normally once I open the app again. Below is my didFinish methods.

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

    // [CONFIGURE Firebase App]
    FirebaseApp.configure()

    // [ENABLE Firebase Persistance]
    Database.database().isPersistenceEnabled = true

    // Setting Api Key for Google maps
    GMSServices.provideAPIKey("AIzaSyDSBDWuW4U3655ifuS6H0LJ9conLrX9_5Q")

    let check =  UserDefaults.standard.value(forKey: "CHECK_LOGIN") as? String

    if check == "YES"
    {
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ROOTVIEW") as! DLDemoRootViewController
        self.window?.rootViewController = nextViewController

    }else
    {
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "loginVIew") as! LoginVC
        self.window?.rootViewController = nextViewController
    }

    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    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 })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    // Register for Remote Notification
    application.registerForRemoteNotifications()


    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]

    return true
}

// [START ios_10_data_message_handling]
extension AppDelegate : MessagingDelegate {

    // [START refresh_token]

    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {

        print("Firebase Refreshed registration token: \(fcmToken)")
        UserDefaults.standard.set(fcmToken, forKey: "token")

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tokenRefresh"), object: nil)
    }
     // [END ios_10_data_message]

    // [START ios_10_data_message]
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set     Messaging.messaging().shouldEstablishDirectChannel to true.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {

        print("Received data message: \(remoteMessage.appData)")
    }
    // [END ios_10_data_message]
}
Toby
  • 9,696
  • 16
  • 68
  • 132
sadhu_sanjay
  • 33
  • 1
  • 6
  • Read my answer to this question https://stackoverflow.com/questions/37899712/fcm-background-notifications-not-working-in-ios/37899773#37899773 – Chris May 29 '17 at 08:55

1 Answers1

0

So I finally found the Issue.

I was overriding the default Initializer of AppDelegate to Configure the Firebase app. after which I was not receiving the notification,

so to fix that I was setting my Apns token inside didRegisterForRemoteNotification after that the notification would start coming, But only temporarily after 24 hours or so it would not come

basically, don't override the default initializer if you unless you really know what you are doing,

sadhu_sanjay
  • 33
  • 1
  • 6