Giter VIP home page Giter VIP logo

godot-android-plugin-firebase's Introduction

Godot (3.2.2) Android plugin for Firebase

This is the updated version of godot-android-module-firebase for the new Godot (3.2.2) Android plugin system (v1).

For the general documentation look there.

Steps to add this plugin to your Godot project are as follows:

  • Open your project in Godot
  • Install Export Templates if necessary
  • Install Android build template
  • Download from this repository's downloads directory both Firebase.release.aar and Firebase.release.gdpa and copy them to [GODOT-PROJECT]/android/plugins/
  • Download from this repository's downloads directory godot-firebase-config.json and copy it to [GODOT-PROJECT]/android/build/assets (create the directory if necessary) and edit it to match your settings (especially AdMob App ID and ad unit IDs)
  • Download your project's google-services.json from Firebase Console and copy it to [GODOT-PROJECT]/android/build/
  • Edit [GODOT-PROJECT]/android/build/build.gradle:
    • add classpath 'com.google.gms:google-services:4.3.3' above the line //CHUNK_BUILDSCRIPT_DEPENDENCIES_BEGIN
    • add apply plugin: 'com.google.gms.google-services' above the line //CHUNK_GLOBAL_BEGIN
    • optional (for Authentication): search for buildTypes.all { and add to this block: resValue "string", "server_client_id", "project-123456..." where project-123456... is the public-facing name of your Firebase project
  • Edit [GODOT-PROJECT]/android/build/AndroidManifest.xml:
    • add the following above the line <!--CHUNK_APPLICATION_BEGIN--> and do not forget to set your AdMob App Id

      <meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID"
          android:value="ca-app-pub-ADMOB_APP_ID"/>
      
  • Edit [GODOT-PROJECT]/android/build/config.gradle and set minSdk to 21 (otherwise a gradle build error occurs: Number of method references cannot exceed 64K)
  • In Godot select menu Project > Export, add Android and edit your settings (package unique name, keystores, etc.) and select under Custom Template: Use Custom Build and also under Plugins: Firebase.

That should be it!

Steps to build .aar from this project:

After checking out this project open Android Studio and start an empty Android project (with no activity, minimum SDK 21). Then select menu File > New > Import module and import this project as a module. In the Android Studio's terminal you can then run:

  • gradlew clean
  • gradlew build

When finished the .aar for both debug and release can be found here: build/outputs/aar

godot-android-plugin-firebase's People

Contributors

yalcin-ata avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

godot-android-plugin-firebase's Issues

Request: Add firebase remote config

Is it possible to add remote firebase configuration in this plugin? I have a project that would need this function but unfortunately I don't understand anything about Java to do this implementation, thanks.

This is not an Issue (C# Implementation solution)

This is my personal implementation, it is not finished but whoever wants to use this plugin in C # will be useful thanks @ yalcin-ata for creating this useful plugin for Godot.

using Godot;
using Godot.Collections;
using System;
using Object = Godot.Object;

public class FirebaseManager : Node
{
    public static FirebaseManager instance;
    private Object Firebase;
    bool initState = false;
    bool isBannerLoaded = false;
    Vector2 bannerSize = Vector2.Zero;
    bool isInterstitialLoaded = false;
    bool isRewardedVideoLoaded = false;
    float gameTimeScale = 0;
    public event FirebaseEvent<Vector2> onBannerActivity;

    uint lastConnectionTry = 0;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        instance = this;

        if (OS.GetName() == "Android")
        {
            InitFirebase();
        }

        gameTimeScale = Engine.TimeScale;
    }

    public override void _Process(float delta)
    {
        if (OS.GetName() == "Android")
        {
            if (!initState)
            {
                if ((OS.GetTicksMsec() - lastConnectionTry) > 10)
                {
                    lastConnectionTry = OS.GetTicksMsec();
                    InitFirebase();
                }
            }
        }
    }

    private void InitFirebase()
    {

        if (Firebase != null)
        {
            Firebase.Call("init", GetInstanceId());
            initState = true;

             // Autentication
             if((bool)Firebase.Call("authentication_google_is_connected") == false){
                 Firebase.Call("authentication_google_sign_in");
             }
        }
        else
        {

            Firebase = Engine.GetSingleton("Firebase");
            Firebase.Call("init", GetInstanceId());
            initState = true;

             // Autentication
             if((bool)Firebase.Call("authentication_google_is_connected") == false){
                 Firebase.Call("authentication_google_sign_in");
             }
        }
    }

    //Firebase implementation
    public void _on_firebase_receive_message(string tag, string from, object key, object data)
    {

        GD.Print($"TAG: {tag} || From: {from} || Key: {key} || Data: {data}");

        switch (from)
        {
            case "AdMob":
                switch ($"{key}")
                {
                    case "Banner":
                        ReceivedBanner(data);
                        break;
                    case "Interstitial":
                        ReceivedInterstitial(data);
                        break;
                    case "RewardedVideo":
                        ReceivedRewardedVideo(data);
                        break;
                }
                break;
        }
    }

    public void AnalyticSendEvents(string eventName, Dictionary dictionary)
    {
        Firebase.Call("analytics_send_events", eventName, dictionary);
    }

    public void AnalyticsSendCustomEvent(string someKey, string someValue)
    {
        Firebase.Call("analytics_send_custom", someKey, someValue);
    }

    private void ReceivedBanner(object data)
    {
        /*
            tag=Firebase from=AdMob key=Banner data=on_ad_loaded
            tag=Firebase from=AdMob key=Banner data=on_ad_failed_to_load:errorCode
        */

        string dataStr = (string)data;

        string[] split = dataStr.Split(':');

        switch (split[0])
        {
            case "on_ad_loaded":
                isBannerLoaded = (bool)Firebase.Call("admob_banner_is_loaded");
                if (isBannerLoaded)
                {

                    // I send the banner measurements through an event.
                    var bz = (Dictionary)Firebase.Call("admob_banner_get_size");

                    var x = (int)bz["width"];
                    var y = (int)bz["height"];

                    //GD.Print($"Banner size x = {x} || Banner size y {y}");

                    Firebase.Call("admob_banner_show", true);
                    onBannerActivity?.Invoke(new Vector2(x, y));
                }
                break;
            case "on_ad_failed_to_load":
                // Send an event with the load failure
                isBannerLoaded = false;

                switch (split[1])
                {
                    case "0":
                        initState = false;
                        break;
                }
                GD.Print($"Banners error {split[1]}");

                Firebase.Call("admob_banner_show", false);
                onBannerActivity?.Invoke(Vector2.Zero);
                break;
        }
    }

    private void ReceivedInterstitial(object data)
    {
        /*
            tag=Firebase from=AdMob key=Interstitial data=on_ad_loaded
            tag=Firebase from=AdMob key=Interstitial data=on_ad_failed_to_load:errorCode
            tag=Firebase from=AdMob key=Interstitial data=on_ad_closed
        */

        string dataStr = (string)data;
        string[] split = dataStr.Split(':');

        switch (split[0])
        {
            case "on_ad_loaded":
                // Show and pause the game
                isInterstitialLoaded = true;
                break;
            case "on_ad_failed_to_load":
                // Send an event with the load failure
                switch (split[1])
                {
                    case "0":
                        initState = false;
                        break;
                }
                isInterstitialLoaded = false;
                break;
            case "on_ad_closed":
                // Reanudar el juego
                isInterstitialLoaded = false;
                break;
        }
    }

    private void ReceivedRewardedVideo(object data)
    {
        /*
            tag=Firebase from=AdMob key=RewardedVideo data={status:on_rewarded_ad_loaded, unit_id:ca-app-pub-AD_UNIT_ID}
            tag=Firebase from=AdMob key=RewardedVideo data={status:on_rewarded_ad_failed_to_load:errorCode, unit_id:ca-app-pub-AD_UNIT_ID}
            tag=Firebase from=AdMob key=RewardedVideo data={status:earned, rewardType, rewardAmount, unit_id:ca-app-pub-AD_UNIT_ID}
            tag=Firebase from=AdMob key=RewardedVideo data={status:closed, unit_id:ca-app-pub-AD_UNIT_ID}
        */

        Dictionary dataDict = (Dictionary)data;
        string status = (string)dataDict["status"];
        string[] split = status.Split(':');

        switch (split[0])
        {
            case "on_rewarded_ad_loaded":
                GD.Print($"Status = {split[0]}");
                isRewardedVideoLoaded = true;
                break;
            case "on_rewarded_ad_failed_to_load":
                switch (split[1])
                {
                    case "0":
                        initState = false;
                        break;
                }
                GD.Print($"Status = {split[0]}:{split[1]}");
                isRewardedVideoLoaded = false;
                break;
            case "earned":
                GD.Print($"Status = {split[0]}");
                isRewardedVideoLoaded = false;
                break;
            case "closed":
                GD.Print($"Status = {split[0]}");
                isRewardedVideoLoaded = false;
                break;
        }
    }
}

public delegate void FirebaseEvent<T>(T data);

Default FirebaseApp failed to initialize because no default options were found

Hey,

I've forked your project, removed everything except analytics and authentication, rebuild it and exported my godot project.
The export itself succeeds, but the app crashes on startup:

2020-07-19 14:58:06.448 12724-12724/com.godot.game W/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.
2020-07-19 14:58:06.448 12724-12724/com.godot.game I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
2020-07-19 14:58:06.600 12724-12724/com.godot.game I/GodotPluginRegistry: Initializing Godot plugin Firebase
2020-07-19 14:58:06.609 12724-12724/com.godot.game I/GodotPluginRegistry: Completed initialization for Godot plugin Firebase
2020-07-19 14:58:08.841 12724-12724/com.godot.game W/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.
2020-07-19 14:58:08.842 12724-12724/com.godot.game D/Firebase: Analytics initializing
2020-07-19 14:58:08.883 12724-12724/com.godot.game D/Firebase: Analytics initialized
2020-07-19 14:58:08.883 12724-12724/com.godot.game D/Firebase: Authentication initializing
2020-07-19 14:58:08.893 12724-12724/com.godot.game D/AndroidRuntime: Shutting down VM
2020-07-19 14:58:08.895 12724-12724/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.godot.game, PID: 12724
    java.lang.IllegalArgumentException: Given String is empty or null
        at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(com.google.android.gms:play-services-basement@@17.1.1:5)
        at com.google.android.gms.auth.api.signin.GoogleSignInOptions$Builder.zac(com.google.android.gms:play-services-base@@17.1.0:55)
        at com.google.android.gms.auth.api.signin.GoogleSignInOptions$Builder.requestIdToken(com.google.android.gms:play-services-base@@17.1.0:29)
        at org.godotengine.androidplugin.firebase.AuthenticationGoogle.init(AuthenticationGoogle.java:68)
        at org.godotengine.androidplugin.firebase.Authentication.init(Authentication.java:52)
        at org.godotengine.androidplugin.firebase.Firebase.initFirebase(Firebase.java:169)
        at org.godotengine.androidplugin.firebase.Firebase.access$100(Firebase.java:37)
        at org.godotengine.androidplugin.firebase.Firebase$1.run(Firebase.java:129)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Note: google-services and firebase plugin json files are included in the export per instructions

I followed all the steps in your instruciton, especially adding dep to google-services and applying google-services plugin in android/build/build.gradle:

sed "/^\/\/CHUNK_BUILDSCRIPT_DEPENDENCIES_BEGIN.*/a classpath 'com.google.gms:google-services:4.3.3'" -i android/build/build.gradle
sed "/^\/\/CHUNK_GLOBAL_BEGIN.*/a apply plugin: 'com.google.gms.google-services'" -i android/build/build.gradle

This results in:

diff --git a/android/build/build.gradle b/android/build/build.gradle
index f1fd716c..a161abe9 100644
--- a/android/build/build.gradle
+++ b/android/build/build.gradle
@@ -17,6 +17,7 @@ buildscript {
         classpath libraries.androidGradlePlugin
         classpath libraries.kotlinGradlePlugin
 //CHUNK_BUILDSCRIPT_DEPENDENCIES_BEGIN
+classpath 'com.google.gms:google-services:4.3.3'
 //CHUNK_BUILDSCRIPT_DEPENDENCIES_END
     }
 }
@@ -115,6 +116,7 @@ android {

     // Both signing and zip-aligning will be done at export time
     buildTypes.all { buildType ->
+resValue "string", "server_client_id", "######"
         buildType.zipAlignEnabled false
         buildType.signingConfig null
     }
@@ -163,4 +165,5 @@ android {
 }

 //CHUNK_GLOBAL_BEGIN
+apply plugin: 'com.google.gms.google-services'
 //CHUNK_GLOBAL_END

Not sure if it's relevant, but after successful export, the apply plugin google-services is missing from build.gradle (along with some other stuff too):

root@557ece530399:/dungeon-puzzle# godot --quiet --export-debug "Android" export/debug.apk
root@557ece530399:/dungeon-puzzle# echo $?
0

root@557ece530399:/dungeon-puzzle# git diff

diff --git a/android/build/build.gradle b/android/build/build.gradle
index f1fd716c..ec454fd4 100644
--- a/android/build/build.gradle
+++ b/android/build/build.gradle
@@ -115,6 +115,7 @@ android {

     // Both signing and zip-aligning will be done at export time
     buildTypes.all { buildType ->
+resValue "string", "server_client_id", "######"
         buildType.zipAlignEnabled false
         buildType.signingConfig null
     }

I've run out of ideas. Does this ring a bell? What should I double-check next?

admob banner issue

admob banner is invisibly shown, we have to click on the ad banner first, then it will show.

[Answers] for "How to show custom icon in the notification?" and "How to receive Firebase Cloud Messaging callbacks?"

I don't know where to submit this, and this repository has been very useful for me. Firebase Cloud Messaging worked like a charm!, but a few things happened before.

I would love if this repository is more visible and more updated by @yalcin-ata ... But I know that every person has it's own business and stuff to do.

These questions are some things that I was able to fix by myself. (In addition to the get_token() method.)

For this, you need to edit your AndroidManifest.xml and then compile following this steps to look like this:

If you want to understand what you are doing read this documentation from Firebase.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application>
    <!-- How to show custom icon in the notification? -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_ic_notification" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

    <!-- Does not change. -->
    <meta-data
        android:name="org.godotengine.plugin.v1.Firebase"
        android:value="org.godotengine.androidplugin.firebase.Firebase" />

    <!-- How to receive Firebase Cloud Messaging callbacks? -->
    <service
        android:name="org.godotengine.androidplugin.firebase.CloudMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>

Godot C# support

Hello sir,
Could you provide a documentation for using your plugin with the C# language as it is fully functional in the 3.2.2 release of godot?

Thanks in advance.

How to create and upload to a folder in firebase storage

I want to upload my files in different folder in firebase storage but I don't know how to do this with this plugin.

Can you please inform me how to create a folder in firebase storage using this plugin and how to upload files to that folder.

Firebase Cloud Msg Icon!

Hey Yalcin, good night! Can you help me with that?
Everythings works fine, but when a send a push notification received a notifi with a gray icon, and i dont find a solution!

Request: Update Firebase Analytics to 17.5.0

Firebase Analytics had manual screen view tracking quit messed up until recently.

  • Before:
    They have the predefined screen_view, but you would get an error if trying to manually send it. You should use setScreenName(), but it had the issue that, due to automatic activity tracking, two screen_view events would get logged. (setScreenName() was not anyway exposed in this plugin. Just mentioning it to tell the whole story.)

  • Now:
    They deprecated setScreenName() and now you can submit screen_view events manually, which gets us rid of the mentioned issues. For that to work, you need Firebase Analytics 17.5.0.

My request is therefore to upgrade the relevant library, like this:

com.google.firebase:firebase-analytics:17.5.0

I've done it for my game and it's working fine.

By the way, since I'm only using Analytics, that's actually the only entry I've left in Firebase.release.gdap. I've even removed androidx.work:work-runtime:2.3.4 and com.google.guava:guava:29.0-android since they don't seem to be needed, at least for it. Are they really needed for the other Firebase packages?


I take the opportunity to thank you for this Firebase integration. It works fine and saves a lot of time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.