Home > Net >  Can I disable Firebase for Unity iOS builds only? (while keeping it in Android builds)
Can I disable Firebase for Unity iOS builds only? (while keeping it in Android builds)

Time:11-20

This question has been asked a couple times with no suitable answer:

How to exclude Firebase Messaging from Unity iOS builds, but not Android builds

How to disable Firebase in Unity for iOS?

I am building a game that uses PlayFab and I need to use Apple's native APNS for iOS and Firebase Messaging for Android. But Firebase is messing up a few things in my iOS builds, and I'm not using any of its functionality anyway.

I tried removing all Firebase references from the podfile, and I tried selecting all the files in Assets/Firebase/Plugins folder and unchecking "iOS" under "Select platforms for plugin". But both resulted in failed builds in Xcode.

Maintaining two separate projects for the same game (one for iOS and one for Android) is not something I want to do so I need a way to keep it in the project without being included in iOS builds.

Edit: The issue I have with Firebase is that after the user enables push notifications on iOS, closes the app completely (force quit), the app crashes on startup every time. And according to the logs it's right before FCM (Firebase Cloud Messaging) debug output.

According to this comment on PlayFab forums, FCM steals push notifications, which is also why I need to disable it.

CodePudding user response:

Finally figured it out. Posting my solution to help others searching (from Google searches it appears many are looking for this with no answer).

To remove only Firebase Cloud Messaging (FCM) from your iOS builds:

  1. Add whatever Firebase modules you want for Android.
    • I added Analytics, Auth, and Messaging.
  2. Add your google-services.json and GoogleService-info.plist files somewhere in your Assets folder (I put mine in the Assets/Firebase folder).
  3. Uncheck "iOS" for messaging plugins in inspector (under "Select platforms for plugin"). Plugins are located in two places:
    • Assets/Firebase/Plugins/iOS/Firebase.Messaging
    • Assets/Plugins/iOS/Firebase/libFirebaseCppMessaging
  4. Remove pod from dependencies file.
    • Open the file Assets/Firebase/Editor/MessagingDependencies and remove the following lines (everything inside the <iosPods> tag):
<iosPod name="Firebase/Messaging" version="8.9.1" minTargetSdk="9.0">
  1. After the Firebase initialization in your code (usually FirebaseApp.CheckAndFixDependenciesAsync()) make sure to use the #if UNITY_ANDROID condition for these lines:
#if UNITY_ANDROID
    FirebaseMessaging.TokenReceived  = OnTokenReceived;
    FirebaseMessaging.MessageReceived  = OnMessageReceived;
#endif

To remove all of Firebase from iOS builds:

  1. Add whatever Firebase modules you want for Android.
    • I added Analytics, Auth, and Messaging.
  2. Add your google-services.json and GoogleService-info.plist files somewhere in your Assets folder (I put mine in the Assets/Firebase folder).
  3. Uncheck "iOS" for all plugin modules in inspector (under "Select platforms for plugin") located in the following folders:
    • Assets/Firebase/Plugins/iOS/
      • In my case, Firebase.Analytics, Firebase.App, Firebase.Auth, and Firebase.Messaging
    • Assets/Plugins/iOS/Firebase/
      • In my case, libFirebaseCppAnalytics, libFirebaseCppApp, libFirebaseCppAuth, and libFirebaseCppMessaging
  4. You also need to uncheck "iOS" for the following files:
    • Assets/Firebase/Plugins/Firebase.Platform
    • Assets/Firebase/Plugins/Firebase.TaskExtension
  5. Remove pod from all dependencies files.
    • Inside the Assets/Firebase/Editor/ folder are several Dependencies files. Open each one of them (there should be one for each module you added, plus an App one) and delete everything inside the <iosPods> tag.
    • In my case, AnalyticsDependencies, AppDependencies, AuthDependencies, and MessagingDependencies
  6. Use the #if UNITY_ANDROID condition to make sure any reference to Firebase is removed for iOS builds.
#if UNITY_ANDROID
  using Firebase;
  using Firebase.Messaging;
#endif
#if UNITY_ANDROID
  FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
    var dependencyStatus = task.Result;
    if (dependencyStatus == DependencyStatus.Available) {

      app = FirebaseApp.DefaultInstance;
  
      firebaseAPIReady = true;
 
      FirebaseMessaging.TokenReceived  = OnTokenReceived;
      FirebaseMessaging.MessageReceived  = OnMessageReceived;
  
    } else {
      Debug.LogError(string.Format(
        "GameManager: Could not resolve all Firebase dependencies: {0}", dependencyStatus));
    }
  });
#endif

The reason my app was crashing:

As I stated above, my app would crash on start, after enabling push notifications in the game, then quitting the app.

Turns out, there's a bug with the Unity IPA (In-App Purchasing) package version 1.4.2 on iOS 15. It has been flagged as an issue and should be fixed in version 1.4.3 but for now, there is a workaround as shown here: https://github.com/Unity-Technologies/com.unity.mobile.notifications/issues/121#issuecomment-956703241

Be sure and add the following directives at the top of the file you create:

using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
  • Related