Search code examples
iosswiftfirebasefirebase-cloud-messagingfirebase-notifications

Firebase Notification not working on Testflight


I have created app with notification. When i send notification on developer mode then i get notifications. but when i send on release mod i don't get anything. I read that i should change certificate to product certificat. But it doesn't helped. here is my appdelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    FIRApp.configure()

    let notificationTypes : UIUserNotificationType = [.alert, .badge, .sound]
    let notificationSettings : UIUserNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(notificationSettings)

    return true
}

private func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    FIRMessaging.messaging().subscribe(toTopic: "/topics/main")
}

private func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
    FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.prod)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print(userInfo["gcm.message_id"]!)
    print(userInfo)
}

func application(_ application: UIApplication, dirtings: UIUserNotificationSettings)
{
    UIApplication.shared.registerForRemoteNotifications()
}

func tokenRefreshNotification(notification: NSNotification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
    }

    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}



func connectToFcm() {
    FIRMessaging.messaging().connect { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}

private func applicationDidEnterBackground(application: UIApplication) {
    FIRMessaging.messaging().disconnect()
    print("Disconnected from FCM.")
}

Solution

  • Just went through this,

    I turned push off and deleted the certs from apple dev center and created them all over again and this made everything work as it should.

    I also had this line of code set to,

    FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.sandbox)
    

    I will change it to .prod when I push to the app store.