2

I had configured my Firebase Cloud Messaging app and everything was running fine until I reinstalled my app. Suddenly, I stopped receiving push notifications from FCM. Obviously my device token has changed, but testing against that specific device token and against all devices (sending a test message from FCM console) didn't work anymore.

Here is my app delegate.m

#import "AppDelegate.h"

#import <Firebase.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
@import GoogleMaps;
@import Firebase;

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Use Firebase library to configure APIs
  [FIRApp configure];
  [FIRMessaging messaging].delegate = self;

  if ([UNUserNotificationCenter class] != nil) {
    // iOS 10 or later
    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
    UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter]
     requestAuthorizationWithOptions:authOptions
     completionHandler:^(BOOL granted, NSError * _Nullable error) {
       // ...
     }];
  } else {
    // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
  }

  [application registerForRemoteNotifications];

  NSURL *jsCodeLocation;

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

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"my_app"
                                               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)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {

  [[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                      NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Error fetching remote instance ID: %@", error);
    } else {
      NSLog(@"Remote instance ID token: %@", result.token);
      NSString* message =
      [NSString stringWithFormat:@"Remote InstanceID token: %@", result.token];
    }
  }];
}

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


@end
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Michał Urbaniak
  • 1,249
  • 13
  • 28
  • What response are you getting in firebase console when you send a test message to the new token? – Derryl Thomas Jun 26 '19 at 09:23
  • I think FCM broke today. I am also trying the same but always getting The request's Authentication (Server-) Key contained an invalid or malformed FCM-Token (a.k.a. IID-Token). Error 401 – Raghav Satyadev Jun 26 '19 at 13:38
  • I am using this API https://fcm.googleapis.com/fcm/send – Raghav Satyadev Jun 26 '19 at 13:38
  • @DerrylThomas I am getting 200 OK when using the firebase admin sdk (in node.js) – Michał Urbaniak Jun 26 '19 at 19:26
  • @MichałUrbaniak can you also check the response body? I have noticed that very often, firebase gives a 200 response but in the body you get something like this(and the notification doesn't get delivered): `{ "multicast_id": 7942205170696355362, "success": 0, "failure": 1, "canonical_ids": 0, "results": [ { "error": "InvalidRegistration" } ] }` – Derryl Thomas Jun 27 '19 at 05:06
  • @RaghavSatyadev, FCM seems to be working fine for me. Not sure if temporarily something happened then. Regardless, the response you mentioned usually comes when you have something wrong with your authorization key. Please check you Authorization header and see if the key mentioned is correct. – Derryl Thomas Jun 27 '19 at 05:12
  • After some experiments with appdelegate.m, FCM is working for me again, but I have no idea which lines of code are crucial. – Michał Urbaniak Jun 27 '19 at 05:41
  • @DerrylThomas I checked everything again, copied server key from their console. copied token directly from android device, replaced google-services.json. but all in vain same error again and again – Raghav Satyadev Jun 28 '19 at 06:59
  • @DerrylThomas I also tried to send notification from console with the token testing, not working – Raghav Satyadev Jun 28 '19 at 07:08
  • @RaghavSatyadev Strange! As a last resort try creating a new server key for your application. Are you using a legacy key by any chance? If it still doesn't work, I suggest posting a new question on SO. – Derryl Thomas Jun 28 '19 at 07:32
  • did all of those things already, not using legacy key. (but tried it also gave invalid registration error) – Raghav Satyadev Jun 28 '19 at 07:35
  • tried with a new project and some online tools like https://www.apnstester.com/fcm/ still same error – Raghav Satyadev Jun 28 '19 at 07:37
  • similar question from Jun 1 https://stackoverflow.com/questions/56408074/the-requests-authentication-server-key-contained-an-invalid-or-malformed-fcm – Raghav Satyadev Jun 28 '19 at 07:40
  • My own question : https://stackoverflow.com/questions/56803033/fcm-error-the-requests-authentication-server-key-contained-an-invalid-or-m – Raghav Satyadev Jun 28 '19 at 07:57

0 Answers0