Untitled

 avatar
unknown
plain_text
24 days ago
3.7 kB
4
Indexable
const { withInfoPlist, withEntitlementsPlist, withAppDelegate } = require('@expo/config-plugins');

/**
 * Config plugin to handle Firebase iOS configuration
 * This plugin ensures proper setup for Firebase Cloud Messaging on iOS
 */
const withFirebaseIOS = (config, options = {}) => {
  // First, update the Info.plist
  config = withInfoPlist(config, (config) => {
    const infoPlist = config.modResults;
    
    // Ensure UIBackgroundModes includes remote-notification
    if (!infoPlist.UIBackgroundModes) {
      infoPlist.UIBackgroundModes = ['remote-notification'];
    } else if (Array.isArray(infoPlist.UIBackgroundModes)) {
      if (!infoPlist.UIBackgroundModes.includes('remote-notification')) {
        infoPlist.UIBackgroundModes.push('remote-notification');
      }
    }
    
    // Add Firebase-specific settings
    infoPlist.FirebaseAppDelegateProxyEnabled = false;
    
    return config;
  });
  
  // Then, update the Entitlements.plist
  config = withEntitlementsPlist(config, (config) => {
    const entitlementsPlist = config.modResults;
    
    // Ensure aps-environment is set
    entitlementsPlist['aps-environment'] = process.env.EAS_BUILD_PROFILE === 'production' 
      ? 'production' 
      : 'development';
    
    return config;
  });
  
  // Finally, update the AppDelegate.mm file to properly handle remote notifications
  config = withAppDelegate(config, (config) => {
    const appDelegate = config.modResults;
    
    // Check if we need to add [application registerForRemoteNotifications]
    if (!appDelegate.contents.includes('[application registerForRemoteNotifications]')) {
      // Add the line after [FIRApp configure]
      appDelegate.contents = appDelegate.contents.replace(
        '[FIRApp configure];',
        '[FIRApp configure];\n  [application registerForRemoteNotifications]; // Added by withFirebaseIOS plugin'
      );
    }
    
    // Check if we need to add [FIRMessaging messaging].APNSToken = deviceToken
    if (!appDelegate.contents.includes('[FIRMessaging messaging].APNSToken = deviceToken')) {
      // Look for the didRegisterForRemoteNotificationsWithDeviceToken method
      const didRegisterMethod = appDelegate.contents.match(
        /- \(void\)application:\(UIApplication \*\)application didRegisterForRemoteNotificationsWithDeviceToken:\(NSData \*\)deviceToken\s*{[^}]*}/
      );
      
      if (didRegisterMethod) {
        // Replace the method with our modified version
        const originalMethod = didRegisterMethod[0];
        const modifiedMethod = originalMethod.replace(
          '{',
          '{\n  [FIRMessaging messaging].APNSToken = deviceToken; // Added by withFirebaseIOS plugin'
        );
        
        appDelegate.contents = appDelegate.contents.replace(originalMethod, modifiedMethod);
      } else {
        // If the method doesn't exist, add it after the @implementation line
        const implementationLine = appDelegate.contents.match(/@implementation AppDelegate/);
        if (implementationLine) {
          const newMethod = `

// Added by withFirebaseIOS plugin
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [FIRMessaging messaging].APNSToken = deviceToken;
  return [super application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
`;
          appDelegate.contents = appDelegate.contents.replace(
            '@implementation AppDelegate',
            '@implementation AppDelegate' + newMethod
          );
        }
      }
    }
    
    return config;
  });
  
  return config;
};

module.exports = withFirebaseIOS;
Editor is loading...
Leave a Comment