Skip to content

Instantly share code, notes, and snippets.

@maheshj01
Last active December 28, 2023 03:41
Show Gist options
  • Save maheshj01/e4a29a387c6e9ad3b71d71d2ca968d0a to your computer and use it in GitHub Desktop.
Save maheshj01/e4a29a387c6e9ad3b71d71d2ca968d0a to your computer and use it in GitHub Desktop.
flutter firebase notes

Set up firebase for flutter using firebase cli: https://firebase.flutter.dev/docs/overview/

Table of Contents

  1. Deploying flutter webapp using Firebase Hosting
  2. Set Firebase messaging notification icon
  3. Send notification to ios simulator/device
  4. CORS issue in firebase storage
  5. Firebase analytics
  6. *Google SignIn

1. Deploying flutter webapp using Firebase Hosting

  1. install npm cli
  2. Install firebase tools using npm
  3. run firebase login

And then you ar ready to deploy to firebase

Maheshs-MacBook-Air-M1:beautiful_snippet mahesh$ flutter pub get
flutteRunning "flutter pub get" in beautiful_snippet...                         902ms
 webMaheshs-MacBook-Air-M1:beautiful_snippet mahesh$ flutter build web

πŸ’ͺ Building with sound null safety πŸ’ͺ

Compiling lib/main.dart for the Web...                                          19.3s
Maheshs-MacBook-Air-M1:beautiful_snippet mahesh$ firebase init

     ######## #### ########  ######## ########     ###     ######  ########
     ##        ##  ##     ## ##       ##     ##  ##   ##  ##       ##
     ######    ##  ########  ######   ########  #########  ######  ######
     ##        ##  ##    ##  ##       ##     ## ##     ##       ## ##
     ##       #### ##     ## ######## ########  ##     ##  ######  ########

You're about to initialize a Firebase project in this directory:

  /Users/mahesh/Documents/flutter_projects/beautiful_snippet

Before we get started, keep in mind:

  * You are initializing in an existing Firebase project directory

? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices. Hosting: Configure a
nd deploy Firebase Hosting sites

=== Project Setup

First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add, 
but for now we'll just set up a default project.

i  .firebaserc already has a default project, using beautiful-snippet.

=== Hosting Setup

Your public directory is the folder (relative to your project directory) that
will contain Hosting assets to be uploaded with firebase deploy. If you
have a build process for your assets, use your build's output directory.

? What do you want to use as your public directory? build/web
? Configure as a single-page app (rewrite all urls to /index.html)? Yes
? Set up automatic builds and deploys with GitHub? No
? File build/web/index.html already exists. Overwrite? No
i  Skipping write of build/web/index.html

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...

βœ”  Firebase initialization complete!
Maheshs-MacBook-Air-M1:beautiful_snippet mahesh$ firebase deploy

=== Deploying to 'beautiful-snippet'...

i  deploying hosting
i  hosting[beautiful-snippet]: beginning deploy...
i  hosting[beautiful-snippet]: found 13 files in build/web
βœ”  hosting[beautiful-snippet]: file upload complete
i  hosting[beautiful-snippet]: finalizing version...
βœ”  hosting[beautiful-snippet]: version finalized
i  hosting[beautiful-snippet]: releasing new version...
βœ”  hosting[beautiful-snippet]: release complete

βœ”  Deploy complete!

Project Console: https://console.firebase.google.com/project/beautiful-snippet/overview
Hosting URL: https://beautiful-snippet.web.app
Maheshs-MacBook-Air-M1:beautiful_snippet mahesh$ 

Firebase hosting by default deploys the app to default site to deploy to a another site you need to change the target. -a site deleted cannot be recovered.

The example below shows how site was deployed to https://vocabhub.web.app when the default site was https://vocabhub-34c7f.web.app

  • Add to firebase.json "site": "vocabhub", e.g after adding firebase.json would like this
{
  "hosting": {
    "public": "build/web",
    "site": "vocabhub",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  } 
}
  • run the deploy to this hosting command
mahesh@Maheshs-MacBook-Air-M1 vocabhub % firebase hosting:sites:list

Sites for project vocabhub-34c7f

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Site ID        β”‚ Default URL                    β”‚ App ID (if set) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ vocabhub-34c7f β”‚ https://vocabhub-34c7f.web.app β”‚ --              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
mahesh@Maheshs-MacBook-Air-M1 vocabhub % firebase deploy --only hosting:vocabhub

=== Deploying to 'vocabhub-34c7f'...

i  deploying hosting
i  hosting[vocabhub]: beginning deploy...
i  hosting[vocabhub]: found 13 files in build/web
βœ”  hosting[vocabhub]: file upload complete
i  hosting[vocabhub]: finalizing version...
βœ”  hosting[vocabhub]: version finalized
i  hosting[vocabhub]: releasing new version...
βœ”  hosting[vocabhub]: release complete

βœ”  Deploy complete!

Project Console: https://console.firebase.google.com/project/vocabhub-34c7f/overview
Hosting URL: https://vocabhub.web.app

2. Set Firebase messaging Notification icon in Android app

originally posted here flutter/flutter#17941 (comment)

<application
        android:name="io.flutter.app.FlutterApplication"
        android:label="When Coin"
        android:icon="@mipmap/ic_launcher">
        
        <!-- Specify the metadata here under applications -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_notification" />
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

3. Send notification to ios simulator/device

Set up push Notifications: https://medium.com/comerge/implementing-push-notifications-in-flutter-apps-aef98451e8f1

sample push notification payload

{
   "aps" : {
      "alert" : {
         "title" : "Game Request",
         "subtitle" : "Five Card Draw",
         "body" : "Bob wants to play poker"
      },
      "category" : "GAME_INVITATION"
   },
   "gameID" : "12345678"
}
 xcrun simctl push booted com.nevercode.triage assets/api.json

4. CORS issue in firebase storage

mahesh@Maheshs-MacBook-Air-M1 ~ % cd google-cloud-sdk 
mahesh@Maheshs-MacBook-Air-M1 google-cloud-sdk % ./install.sh
Welcome to the Google Cloud CLI!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. This data is
handled in accordance with our privacy policy
<https://cloud.google.com/terms/cloud-privacy-notice>. You may choose to opt in this
collection now (by choosing 'Y' at the below prompt), or at any time in the
future by running the following command:

    gcloud config set disable_usage_reporting false

Do you want to help improve the Google Cloud CLI (y/N)?  y


Your current Google Cloud CLI version is: 392.0.0
The latest available version is: 392.0.0

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                 Components                                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     Status    β”‚                         Name                         β”‚            ID            β”‚   Size   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Not Installed β”‚ App Engine Go Extensions                             β”‚ app-engine-go            β”‚  4.0 MiB β”‚
β”‚ Not Installed β”‚ Appctl                                               β”‚ appctl                   β”‚ 18.5 MiB β”‚
β”‚ Not Installed β”‚ Artifact Registry Go Module Package Helper           β”‚ package-go-module        β”‚  < 1 MiB β”‚
β”‚ Not Installed β”‚ Cloud Bigtable Command Line Tool                     β”‚ cbt                      β”‚  9.3 MiB β”‚
β”‚ Not Installed β”‚ Cloud Bigtable Emulator                              β”‚ bigtable                 β”‚  5.7 MiB β”‚
β”‚ Not Installed β”‚ Cloud Datalab Command Line Tool                      β”‚ datalab                  β”‚  < 1 MiB β”‚
β”‚ Not Installed β”‚ Cloud Datastore Emulator                             β”‚ cloud-datastore-emulator β”‚ 34.9 MiB β”‚
β”‚ Not Installed β”‚ Cloud Firestore Emulator                             β”‚ cloud-firestore-emulator β”‚ 39.5 MiB β”‚
β”‚ Not Installed β”‚ Cloud Pub/Sub Emulator                               β”‚ pubsub-emulator          β”‚ 60.7 MiB β”‚
β”‚ Not Installed β”‚ Cloud Run Proxy                                      β”‚ cloud-run-proxy          β”‚  7.4 MiB β”‚
β”‚ Not Installed β”‚ Cloud SQL Proxy                                      β”‚ cloud_sql_proxy          β”‚  7.3 MiB β”‚
β”‚ Not Installed β”‚ Google Cloud Build Local Builder                     β”‚ cloud-build-local        β”‚  6.2 MiB β”‚
β”‚ Not Installed β”‚ Google Container Registry's Docker credential helper β”‚ docker-credential-gcr    β”‚          β”‚
β”‚ Not Installed β”‚ Kustomize                                            β”‚ kustomize                β”‚  7.4 MiB β”‚
β”‚ Not Installed β”‚ Minikube                                             β”‚ minikube                 β”‚ 27.1 MiB β”‚
β”‚ Not Installed β”‚ Nomos CLI                                            β”‚ nomos                    β”‚ 23.5 MiB β”‚
β”‚ Not Installed β”‚ On-Demand Scanning API extraction helper             β”‚ local-extract            β”‚ 10.9 MiB β”‚
β”‚ Not Installed β”‚ Skaffold                                             β”‚ skaffold                 β”‚ 19.2 MiB β”‚
β”‚ Not Installed β”‚ anthos-auth                                          β”‚ anthos-auth              β”‚ 18.2 MiB β”‚
β”‚ Not Installed β”‚ config-connector                                     β”‚ config-connector         β”‚ 52.7 MiB β”‚
β”‚ Not Installed β”‚ gcloud Alpha Commands                                β”‚ alpha                    β”‚  < 1 MiB β”‚
β”‚ Not Installed β”‚ gcloud Beta Commands                                 β”‚ beta                     β”‚  < 1 MiB β”‚
β”‚ Not Installed β”‚ gcloud app Java Extensions                           β”‚ app-engine-java          β”‚ 51.9 MiB β”‚
β”‚ Not Installed β”‚ gcloud app PHP Extensions                            β”‚ app-engine-php           β”‚ 21.9 MiB β”‚
β”‚ Not Installed β”‚ gcloud app Python Extensions                         β”‚ app-engine-python        β”‚  7.8 MiB β”‚
β”‚ Not Installed β”‚ gcloud app Python Extensions (Extra Libraries)       β”‚ app-engine-python-extras β”‚ 26.4 MiB β”‚
β”‚ Not Installed β”‚ kpt                                                  β”‚ kpt                      β”‚ 12.6 MiB β”‚
β”‚ Not Installed β”‚ kubectl                                              β”‚ kubectl                  β”‚  < 1 MiB β”‚
β”‚ Not Installed β”‚ kubectl-oidc                                         β”‚ kubectl-oidc             β”‚ 18.2 MiB β”‚
β”‚ Not Installed β”‚ pkg                                                  β”‚ pkg                      β”‚          β”‚
β”‚ Installed     β”‚ BigQuery Command Line Tool                           β”‚ bq                       β”‚  1.6 MiB β”‚
β”‚ Installed     β”‚ Cloud Storage Command Line Tool                      β”‚ gsutil                   β”‚ 15.5 MiB β”‚
β”‚ Installed     β”‚ Google Cloud CLI Core Libraries                      β”‚ core                     β”‚ 23.8 MiB β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
To install or remove components at your current SDK version [392.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [392.0.0], run:
  $ gcloud components update



To take a quick anonymous survey, run:
  $ gcloud survey


Modify profile to update your $PATH and enable shell command completion?

Do you want to continue (Y/n)?  y

The Google Cloud SDK installer will now prompt you to update an rc file to bring
 the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use 
[/Users/mahesh/.zshrc]:                 
Backing up [/Users/mahesh/.zshrc] to [/Users/mahesh/.zshrc.backup].
[/Users/mahesh/.zshrc] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts


mahesh@Maheshs-MacBook-Air-M1 google-cloud-sdk % gcloud init
Welcome! This command will take you through the configuration of gcloud.

Settings from your current configuration [cors] are:
core:
  account: email1@example.com
  disable_usage_reporting: 'True'
  project: furdle-21410493

Pick configuration to use:
 [1] Re-initialize this configuration [cors] with new settings 
 [2] Create a new configuration
 [3] Switch to and re-initialize existing configuration: [default]
Please enter your numeric choice:  3

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                            
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

Choose the account you would like to use to perform operations for this 
configuration:
 [1] email1@example.com
 [2] email2@example.com
 [3] Log in with a new account
Please enter your numeric choice:  2

You are logged in as: [email1@example.com].

Pick cloud project to use: 
 [1] project-1
 [2] project-2
 [3] project-3
 ...
 ...
 [14] Project-14
 [15] 
 [16] 
Please enter numeric choice or text value (must exactly match list item):  14

Your current project has been set to: [Project-14].

Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.

Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use username@gmail.com by default
* Commands will reference project `Project-14` by default
Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.
mahesh@Maheshs-MacBook-Air-M1 myproject % gcloud --help
mahesh@Maheshs-MacBook-Air-M1 myproject % gsutil cors set cors.json gs://myproject-id.appspot.com
Setting CORS on gs://myproject-id.appspot.com/...
mahesh@Maheshs-MacBook-Air-M1 myproject % 

5. Firebase analytics

  • Add routeObserver to MaterialApp
 navigatorObservers: [
            FirebaseAnalyticsObserver(analytics: firebaseAnalytics),
 ],
  • Send events to firebase using firebaseAnalytics e.g firebaseAnalytics.setScreen(name:'screenName');
  • To test in realtime use debugView

enable debug mode

adb shell setprop debug.firebase.analytics.app <package_name> 

Disable debug mode

adb shell setprop debug.firebase.analytics.app .none.

6. Google SignIn

Generate SHA fingerprint (Mac)

keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment