invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.71k stars 2.22k forks source link

🔥Internal error encountered FCM when sending notification to iOS #1639

Closed williamgoodhew closed 5 years ago

williamgoodhew commented 6 years ago

Issue

I am trying to send push notifications to an iOS app installed via TestFlight.

I can't get push notifications working with iOS react-native-firebase. The client is asking for permission correctly, and the pushToken is being stored correctly in the Firestore DB. I can send a notification to an Android device. I have tried to send notifications to iOS manually in the firebase console manually to a single device. I could not find any good logs but the status was: Failed - Invalid Team ID, Key ID and APNs Auth Key combination.

I tried sending a notification through cloud functions to get more detailed errors and I got the following error:

{ Error: Internal error encountered.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16)
at FirebaseMessagingRequestHandler.handleHttpError (/user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:125:50)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:113:23
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
errorInfo: 
{ code: 'messaging/internal-error',
message: 'Internal error encountered.' },
codePrefix: 'messaging' }

here is the code for the cloud function which is working for Android but not on iOS:

export const testNotification =
functions.https.onRequest((req, res) => {
  const message = {
        notification: {
            title: 'hello ios',
            body: "why won't you work",
        },
    token: 'czZaY1uryJs:APA91b***************'
    };

  return admin.messaging()
    .send(message)
    .then((response) => {
      console.log("Successfully sent message:", response);
    })
    .catch((error) => {
      console.log("Error sending message:", error);
    });
})

I have added some screenshots of the key set up that I have at the bottom of this post


Project Files

iOS

ios/Podfile:

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'Athalens' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!
  pod 'Firebase'
  pod 'FirebaseCore'
  pod 'FirebaseAuth'
  pod 'FirebaseDatabase'
  pod 'FirebaseStorage'
  pod 'FirebaseFirestore'
  pod 'Firebase/Messaging', '~> 5.9.0'
  # Pods for Athalens

  target 'AthalensTests' do
    inherit! :search_paths
    # Pods for testing
  end

end

target 'Athalens-tvOS' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for Athalens-tvOS

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 "RNFirebaseNotifications.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  [RNFirebaseNotifications configure];
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"Athalens"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  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];
  }

@end

Android

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

Environment

screenshot 2018-10-25 at 17 27 48 screenshot 2018-10-25 at 17 29 02 screenshot 2018-10-25 at 17 30 11

Think react-native-firebase is great? Please consider supporting the project with any of the below:

Aung-Myint-Thein commented 6 years ago

Oooh.. is it a bug or feature request?

williamgoodhew commented 6 years ago

This is a bug.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Aung Myint Thein notifications@github.com Sent: Thursday, November 1, 2018 7:19:46 AM To: invertase/react-native-firebase Cc: William Goodhew; Author Subject: Re: [invertase/react-native-firebase] 🔥Internal error encountered FCM when sending notification to iOS (#1639)

Oooh.. is it a bug or feature request?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/invertase/react-native-firebase/issues/1639#issuecomment-434943053, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AfSilSVww8-cgYX9b4gbnKezRRo2d-WBks5uqpKCgaJpZM4X6qcG.

Aung-Myint-Thein commented 6 years ago

Oh does it mean we still cannot use react native firebase for iOS push notifications..? I am also getting the same error but this is first time implementation for me and I thought it was my mistake in somewhere.

williamgoodhew commented 6 years ago

@Salakar you have put an await-user-feedback tag on this - what feedback do you need from me?

Aung-Myint-Thein commented 6 years ago

I have managed to make this work by installing all the keys and certificates. Moreover, you can also try to send the push notification to the device from FCM API. It will provide more concrete and raw errors. For me, it says "InvalidAPNsCredentials" then I tried to set up the APN certificate again :)

Try to send the push to your device from fcm api here https://stackoverflow.com/questions/37371990/how-can-i-send-a-firebase-cloud-messaging-notification-without-use-the-firebase

williamgoodhew commented 6 years ago

Thanks @Aung-Myint-Thein - just for clarification - what keys and certificates did you install and where?

williamgoodhew commented 6 years ago

hi @Salakar just wondering if you guys had any suggestions?

Aung-Myint-Thein commented 6 years ago

Ah, I downloaded a new set of provisional files for dev and distribution to run in xcode. Then, I downloaded a new set of APNSKey.

gglee89 commented 5 years ago

@williamgoodhew I'm experiencing the exact same scenario.

I am able to send messages through FCM on Android but on iOS I get the internal error message.

{ Error: Internal error encountered.
    at FirebaseMessagingError.FirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseMessagingError (/app/node_modules/firebase-admin/lib/utils/error.js:241:16)
    at Function.FirebaseMessagingError.fromServerError (/app/node_modules/firebase-admin/lib/utils/error.js:271:16)
    at FirebaseMessagingRequestHandler.handleHttpError (/app/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:125:50)
    at /app/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:113:23
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  errorInfo:
   { code: 'messaging/internal-error',
     message: 'Internal error encountered.' },
  codePrefix: 'messaging' }

Podfile

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'Rocky' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for Rocky
  # Required by RNFirebase
  pod 'Firebase'
  pod 'Firebase/Auth'
  pod 'Firebase/Core'
  pod 'Firebase/Database'
  pod 'Firebase/Messaging', '~> 5.11.0'

  pod 'React', :path => '../node_modules/react-native', :subspecs => [
    'RCTImage', # <-- Add RCTImage
  ]
  pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

  pod 'RNSVG', :path => '../node_modules/react-native-svg'
  pod 'ReactNativePermissions', :path => '../node_modules/react-native-permissions'

  pod 'react-native-keep-awake', :path => '../node_modules/react-native-keep-awake'

  pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'

  pod 'RNSnackbar', :path => '../node_modules/react-native-snackbar'

  post_install do |installer|
    installer.pods_project.targets.each do |target|

      # The following is needed to ensure the "archive" step works in XCode.
      # It removes React & Yoga from the Pods project, as it is already included in the main project.
      # Without this, you'd see errors when you archive like:
      # "Multiple commands produce ... libReact.a"
      # "Multiple commands produce ... libyoga.a"

      targets_to_ignore = %w(React yoga)

      if targets_to_ignore.include? target.name
        target.remove_from_project
      end

    end
  end

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 <Firebase.h>
#import "AppDelegate.h"
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

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

@implementation AppDelegate

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

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  // jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

  [FIRApp configure];
  [RNFirebaseNotifications configure];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"Rocky"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  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];
}

@end

Environment

williamgoodhew commented 5 years ago

I had an error in my bundle identifier which was only picked up using this package. My bundle identifier had a capital letter in accidentally, and that caused the error.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Gustavo Lee notifications@github.com Sent: Wednesday, November 21, 2018 9:19:20 AM To: invertase/react-native-firebase Cc: William Goodhew; Mention Subject: Re: [invertase/react-native-firebase] 🔥Internal error encountered FCM when sending notification to iOS (#1639)

@williamgoodhewhttps://github.com/williamgoodhew I'm experiencing the exact same scenario.

I am able to get messages through FCM on Android but on iOS I get the internal error message.

{ Error: Internal error encountered. at FirebaseMessagingError.FirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseMessagingError (/app/node_modules/firebase-admin/lib/utils/error.js:241:16) at Function.FirebaseMessagingError.fromServerError (/app/node_modules/firebase-admin/lib/utils/error.js:271:16) at FirebaseMessagingRequestHandler.handleHttpError (/app/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:125:50) at /app/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:113:23 at at process._tickCallback (internal/process/next_tick.js:188:7) errorInfo: { code: 'messaging/internal-error', message: 'Internal error encountered.' }, codePrefix: 'messaging' }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/invertase/react-native-firebase/issues/1639#issuecomment-440592421, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AfSilfgQnLT323fjwIl4XfdfxG9hQCtaks5uxRqYgaJpZM4X6qcG.

gglee89 commented 5 years ago

@williamgoodhew Thank you for your comments.

I my case, I had my bundle identifier name all lowercased. So I could assume from the start that it was not what was generating the Firebase internal-error.

Fortunately I got it to work, but it took me few hours to figure out why. It was a very dummy mistake, but I forgot to upload my APN Authentication Key to Firebase Console.

Just for the sake of having a visual representation, if you go to Firebase Console > Project Settings > Cloud Messaging:

screen shot 2018-11-22 at 12 12 56 pm

And because I didn't have that it basically caused the unfriendly previously mentioned internal-error. Just to repeat what the message was:

...
{ code: 'messaging/internal-error',
     message: 'Internal error encountered.' },
  codePrefix: 'messaging' }

To have a better understanding of how iOS operates, I looked at the official video inside this post from Firebase: https://firebase.googleblog.com/2017/01/debugging-firebase-cloud-messaging-on.html

Now that you gain a overall understanding of how iOS works, then I followed the instructions precisely as it is described in the official react-native-firebase documentation here: https://rnfirebase.io/docs/v5.x.x/messaging/ios

And Voilá. The sensitive point with iOS here is on how you organize and properly assign generated APN Certificates and Keys.

(quick update) Make sure the generated token is not expired. That also caused me the exact same error output from Firebase.

Check item no.2 here https://rnfirebase.io/docs/v5.x.x/messaging/device-token

Hope it helps.

stale[bot] commented 5 years ago

Hello 👋, to help manage issues we automatically close stale issues. This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 5 years ago

Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.