APNS device token not set before retrieving FCM Token for Sender ID - React Native Firebase

24,109

Solution 1

I was able to fix the issue: it was quite simple honestly. My iPhone has had an issue with connecting to the internet, and I fixed it and this issue got fixed too! :)

Solution 2

For anyone else, this warning/error will also appear if you've forgotten to add the Push Notification capability in the Signing & Capabilities tab of your project target in Xcode

Solution 3

I solved this problem in 3 days. You need to connect the p8 key to your firebase

Link to create a p8 key https://stackoverflow.com/a/67533665/13033024

enter image description here

Add the following code to your AppDelegate.swift

 if #available(iOS 10.0, *) { 
     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
   
 application.registerForRemoteNotifications()

Go to xCode and click on the "Capability" tab. Add Background Modes and Push Notifications. In the Background Modes tab, enable Background fetch and Remote notifications

Don't forget to add it for release, debug, profile

enter image description here

Remove your app from your phone and run it again.

I hope this will help you too!

Solution 4

Maybe someone had mentioned before... You must use real device, not simulator, or this error will show always.

Solution 5

After wasting my whole day trying to find a fix for this weird bug, I found a solution that fixed it for me. In my case, Cloud Messaging somehow screwed up the APN Token setup process, so I had to manually set it myself:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Registered for Apple Remote Notifications")
    Messaging.messaging().setAPNSToken(deviceToken, type: .unknown)
}

Of course you have to register for remote notifications beforehand to even receive notifications via application.registerForRemoteNotifications()

Share:
24,109
Prabash Darshana
Author by

Prabash Darshana

Updated on February 18, 2022

Comments

  • Prabash Darshana
    Prabash Darshana over 2 years

    I have been following this tutorial to set-up Remote Push notifications on my react-native application using react-native-firebase Version 5.2.0. After I configured everything and ran the application I get an error as:

    APNS device token not set before retrieving FCM Token for Sender ID ''. Notifications to this FCM Token will not be delievered over APNS. Be sure to re-retrieve the FCM token once the APNS token is set.

    Been trying to figure out how to solve this issue but wasn't quite successful. Running on react-native : 0.61.2, react-native-firebase: 5.2.0

    Following is my AppDelegate.m

    #import "AppDelegate.h"
    
    #import <React/RCTBridge.h>
    #import <React/RCTBundleURLProvider.h>
    #import <React/RCTRootView.h>
    #import <RNCPushNotificationIOS.h>
    #import <UserNotifications/UserNotifications.h>
    #import <Firebase.h>
    
    @import Firebase;
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      [FIRApp configure];
      RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
      RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                       moduleName:@"helloworld"
                                                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];
    
      // define UNUserNotificationCenter
      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
      center.delegate = self;
    
      return YES;
    }
    
    - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
    {
    #if DEBUG
      return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
    #else
      return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
    #endif
    }
    
    // Required to register for notifications
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    {
      [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
    }
    // Required for the register event.
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
      [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    }
    // Required for the notification event. You must call the completion handler after handling the remote notification.
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
      [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    }
    // Required for the registrationError event.
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
      [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
    }
    // Required for the localNotification event.
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
      [RNCPushNotificationIOS didReceiveLocalNotification:notification];
    }
    
    //Called when a notification is delivered to a foreground app.
    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    {
      NSLog(@"User Info : %@",notification.request.content.userInfo);
      completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
    }
    
    
    @end
    

    and token retrieval on my App.js:

    const messaging = firebase.messaging();
    
    messaging.hasPermission()
      .then((enabled) => {
        if (enabled) {
          messaging.getToken()
            .then(token => { console.log("+++++ TOKEN ++++++" + token) })
            .catch(error => { console.log(" +++++ ERROR GT +++++ " + error) })
        } else {
          messaging.requestPermission()
            .then(() => { console.log("+++ PERMISSION REQUESTED +++++") })
            .catch(error => { console.log(" +++++ ERROR RP ++++ " + error) })
        }
    
      })
      .catch(error => { console.log(" +++++ ERROR +++++ " + error) });
    
    

    Any help would be much appreciated! Thanks!

  • A. Shukri
    A. Shukri over 2 years
    can u share the code ?
  • Albert Hidalgo
    Albert Hidalgo over 2 years
    For my case, I missed the Background Modes Thanks!
  • Nurudin Lartey
    Nurudin Lartey about 2 years
    I had everything in place except for the Background Modes. Thanks very much @You Love
  • Md. Sulayman
    Md. Sulayman about 2 years
    It didn't work in my case.
  • Zorayr
    Zorayr about 2 years
    This fixes it. It's either a bug with their swizzling or just bad documentation from firebase.