Useful or not, from you.
react-native-firebase 🔥firebase.messaging().ios.getAPNSToken() always returns null

Issue

On the server side we are using amazon sns service for the fcm communication with the devices, so the token returned from firebase.messaging().getToken() is just too long. So we've figured we should get the token with getAPNStoken call

import firebase from 'react-native-firebase'

const token = await firebase.messaging().ios.getAPNSToken() // null

only that this call returns null no matter what I try, the documentation isn't very clear about how to use and use cases, but I guess that it should be inferred from the name. anyhow, I've also tried to register first using

firebase.messaging().ios.registerForRemoteNotifications().then(() => 
const token = firebase.messaging().ios.getAPNSToken()) // null

still no success. Am I doing something wrong, or is that a bug?

Project Files

iOS

ios/Podfile:

  • [ ] I'm not using Pods
  • [x] I'm using Pods and my Podfile looks like:
# Uncomment the next line to define a global platform for your project
platform :ios, '10.0'

require_relative '../node_modules/@unimodules/core/podfile-macro'

# The target name is most likely the name of your project.
target 'broadcaster' do

  # Your 'node_modules' directory is probably in the root of your project,
  # but if not, adjust the `:path` accordingly
  pod 'React', :path => '../node_modules/react-native', :subspecs => [
    'Core',
    'CxxBridge', # Include this for RN >= 0.47
    'DevSupport', # Include this to enable In-App Devmenu if RN >= 0.43
    'RCTText',
    'RCTNetwork',
    'RCTWebSocket', # Needed for debugging
    'RCTAnimation', # Needed for FlatList and animations running on native UI thread
    'RCTImage'
    # Add any other subspecs you want to use in your project
  ]
  # Explicitly include Yoga if you are using RN >= 0.42.0
  pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

  # Third party deps podspec link
  pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
  pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
  pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons'
  pod 'EXFont', path: '../node_modules/expo-font/ios'
  pod 'EXImagePicker', path: '../node_modules/expo-image-picker/ios'
  pod 'react-native-orientation-locker', :path => '../node_modules/react-native-orientation-locker/react-native-orientation-locker.podspec'
  pod 'EXAV', path: '../node_modules/expo-av/ios'
  pod 'RNImageCropPicker', :path =>  '../node_modules/react-native-image-crop-picker'
  pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
  pod 'Firebase/Core', '~> 5.15.0'
  pod 'Firebase/Messaging', '~> 5.15.0'
  use_unimodules!


end

AppDelegate.m:

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

#import <EXCore/EXModuleRegistry.h>
#import <EXReactNativeAdapter/EXNativeModulesProxy.h>
#import <EXReactNativeAdapter/EXModuleRegistryAdapter.h>
#import "Orientation.h"
#import <Firebase.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  [RNFirebaseNotifications configure];
  self.moduleRegistryAdapter = [[EXModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[EXModuleRegistryProvider alloc] init]];
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"broadcaster" initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
  [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
{
  NSArray<id<RCTBridgeModule>> *extraModules = [_moduleRegistryAdapter extraModulesForBridge:bridge andExperience:nil];
  // If you'd like to export some custom RCTBridgeModules that are not Expo modules, add them here!
  return extraModules;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  return [Orientation getOrientation];
}

@end

Environment

<!-- change [ ] to [x] to select an option(s) -->

  • Platform that you're experiencing the issue on:
    • [] iOS
    • [ ] Android
    • [x] iOS but have not tested behavior on Android
    • [ ] Android but have not tested behavior on iOS
    • [ ] Both
  • If known, the version of the platform are you experiencing the issue on:
    • ADD_SOMETHING_HERE e.g. iOS 10 or Android API 28
  • Operating System:
    • [x] MacOS, version: N/A
    • [ ] Windows, version: N/A
    • [ ] Other, please specify: N/A
  • Build Tools:
    • Xcode 10
  • React Native version:
    • 0.57.3
  • **React Native Firebase library version:
    • 5.2.3
  • Firebase module(s) you're using that has the issue:
    • [x] N/A
    • [ ] Authentication
    • [ ] Analytics
    • [ ] Cloud Firestore
    • [x] Cloud Messaging (FCM)
    • [ ] Crashlytics
    • [ ] Dynamic Links
    • [ ] Functions Callable
    • [ ] In App Messaging
    • [ ] Indexing
    • [ ] Invites
    • [ ] Instance ID
    • [ ] ML Kit
    • [ ] Notifications
    • [ ] Performance Monitoring
    • [ ] Realtime Database
    • [ ] Remote Config
    • [ ] Storage
  • Are you using TypeScript?
    • [ ] No
    • [x] Yes, version: N/A
  • Are you using Expo, e.g. ExpoKit?
    • [ ] No
    • [ ] Yes, I've not ejected
    • [ ] Yes, but I have ejected to ExpoKit
    • [x] Yes, but I have ejected to vanilla React Native
That's a useful answer
Without any help

Add this function to AppDelegate.m:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [FIRMessaging messaging].APNSToken = deviceToken;
}

Hope this will be helpful.