react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

getInitialNotification() returns undefined on iOS

Open murat-mehmet opened this issue 9 years ago • 7 comments

I have made everything shown on documentation correctly and still when the app is closed and I click on fcm notification, I get undefined getInitialNotification() on iOS.

Heres is my AppDelegate.m

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

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

#import "RCCManager.h"

#import <FBSDKCoreKit/FBSDKCoreKit.h>

#import <Firebase.h>

#import "RNFirebaseMessaging.h"


@implementation AppDelegate

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;
  
#ifdef DEBUG
  //  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
#else
  jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
  
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  self.window.backgroundColor = [UIColor whiteColor];
  [[RCCManager sharedInstance] initBridgeWithBundleURL:jsCodeLocation];

  [FIRApp configure];
  [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
  
  [self registerForRemoteNotifications];
  
  return YES;
}


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

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
  [RNFirebaseMessaging didReceiveRemoteNotification:userInfo];
}

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


- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  [RNFirebaseMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}

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


-(void)registerForRemoteNotifications {
  
  if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
    
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    
    center.delegate = self;
    
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
      
      if(!error){
        [[UIApplication sharedApplication] registerForRemoteNotifications];
      }
      
    }];
    
  }
  else {
    // Code for old versions
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    
                                                    UIUserNotificationTypeBadge |
                                                    
                                                    UIUserNotificationTypeSound);
    
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                            
                                                                             categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
  }
}



- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  
  
  
  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                openURL:url
                                                      sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                             annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
                  ];
  // Add any custom logic here.
  return handled;
}

@end

On android it works perfectly.

Environment

  1. Target Platform (e.g. iOS, Android): iOS
  2. Development Operating System (e.g. macOS Sierra, Windows 10): maxOS
  3. Build tools (Xcode or Android Studio version, iOS or Android SDK version, if relevant): xcode
  4. React Native version (e.g. 0.45.1): 0.45.1
  5. RNFirebase Version (e.g. 2.0.2): 2.0.4

murat-mehmet avatar Jul 26 '17 10:07 murat-mehmet

@CoderProject Sorry for the delay on this, are you still experiencing issues?

chrisbianca avatar Sep 23 '17 15:09 chrisbianca

Yes, still having the problem. When my app is completely closed on iOS and the notification with data message is clicked, there's no way that I can handle the data that is sent with the notification.

What am I doing wrong?

murat-mehmet avatar Sep 26 '17 13:09 murat-mehmet

same here ... data returns undefined

creative-git avatar Oct 03 '17 08:10 creative-git

+1 same issue here with:

  1. Target Platform (e.g. iOS, Android): iOS
  2. Development Operating System (e.g. macOS Sierra, Windows 10): macOS High Sierra
  3. Build tools (Xcode or Android Studio version, iOS or Android SDK version, if relevant): XCode 9.0.1
  4. React Native version (e.g. 0.45.1): 0.49.3
  5. RNFirebase Version (e.g. 2.0.2): 3.0.4

It's flaky, and sometimes I get a message object, but more often than not I get undefined. Tried calling getInitialMessage() from several places:

  • Before class declaration of root component
  • In componentDidMount() of root component
  • In AppState change event handler e.g. AppState.addEventListener('change', () => /* ... */)
    • change event listener added in componentDidMount() of root component

ashour avatar Oct 27 '17 18:10 ashour

I also get undefined, except when I open the app from a notification..

messaging.getInitialNotification().then(notif=>{
      if(!notif) {
        // Whenever I reload the app or open normally
      } else {
        // When I open the app FROM a push notification (app is closed when I get the push)
      }
    });

kevando avatar Oct 29 '17 20:10 kevando

Thanks for reporting. We're aware of lots of issues with notifications and will be addressing them all as part of the v3.2.0 release stream. Please see https://github.com/invertase/react-native-firebase/issues/595 for updates.

chrisbianca avatar Nov 08 '17 15:11 chrisbianca

Good news, the long awaited alpha of our messaging and notifications overhaul is now available!!

Check out the release notes here: https://github.com/invertase/react-native-firebase/releases/tag/v4.0.0-alpha.1

If you have any comments and suggestions or want to report an issue, come find us on Discord

chrisbianca avatar Mar 20 '18 09:03 chrisbianca