Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Phone auth fails to pick up on remote notifications registration

Currently struggling with the following error showing up in logs:

<Error> [Firebase/Auth][I-AUT000015] The UIApplicationDelegate must handle remote notifcation for phone number authentication to work.

as well as this NSError object when calling verifyPhoneNumber:completion: :

@"NSLocalizedDescription" : @"If app delegate swizzling is disabled, remote notifications received by UIApplicationDelegate need to be forwarded to FIRAuth's canHandleNotificaton: method." @"error_name" : @"ERROR_NOTIFICATION_NOT_FORWARDED"

Anyone know what this is about and how it can be solved?

I am using XCode 8.3.3, Firebase 4.0.0, I've switched OFF swizzling, and I already confirmed that I successfully register both for APNS (I see the token) and for FCM (I see this as well).

I used the sample code both in documentation and in the iOS cloud message github sample repo.

Before trying to integrate Firebase in the same project, I had Digits phone auth working flawlessly, and push notifications coming in both from AWS SNS and OneSignal.

Relevant code for Firebase-related parts:

AppDelegate

+ (void)initialize
{

    FIROptions *firOptions = [[FIROptions alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"some-plist-name" ofType:@"plist"]];
    [FIRApp configureWithOptions:firOptions];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [FIRMessaging messaging].delegate = self;
    [FIRMessaging messaging].shouldEstablishDirectChannel = YES;

    [UNUserNotificationCenter currentNotificationCenter].delegate = self;

    // do some other setup stuff here ...

    return YES;
}

#pragma mark - FIRMessagingDelegate

- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {

    // I get an fcmToken here as expected and notify the the relevant controller(s)

}

- (void)messaging:(nonnull FIRMessaging *)messaging didReceiveMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage {

}

#pragma mark - UNUserNotificationCenterDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    [[FIRAuth auth] canHandleNotification:userInfo];

    completionHandler(UNNotificationPresentationOptionNone);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {  
    completionHandler();
}

#pragma mark - Notifications

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

    FIRMessagingAPNSTokenType tokenType = FIRMessagingAPNSTokenTypeProd;
#if DEBUG && !TESTFLIGHT
    tokenType = FIRMessagingAPNSTokenTypeSandbox;
#endif

    [[FIRMessaging messaging] setAPNSToken:deviceToken type:tokenType];

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    [[FIRAuth auth] canHandleNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    [[FIRAuth auth] canHandleNotification:userInfo];

}

- (void)application:(UIApplication *) application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void (^)())completionHandler {

    completionHandler();

}


@end

Controller 1

...

UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {

}];

[[UIApplication sharedApplication] registerForRemoteNotifications];

...

Controller 2 - after receiving APNS and FCM OK

...

[[FIRPhoneAuthProvider provider] verifyPhoneNumber:@"+11111111"
                                        completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
    // Here is where I get that error.
}];
...
like image 386
whycodewhyyyy Avatar asked Dec 10 '22 11:12

whycodewhyyyy


1 Answers

you have missed to add this method in your AppDelegate

func application(_ application: UIApplication,
                 didReceiveRemoteNotification notification: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if Auth.auth().canHandleNotification(notification) {
        completionHandler(.noData)
        return
    }
    // This notification is not auth related, developer should handle it.
    handleNotification(notification)
}
like image 200
Hashem Aboonajmi Avatar answered Dec 14 '22 23:12

Hashem Aboonajmi