Android Question Firebase

Declan

Well-Known Member
Licensed User
Longtime User
I am attempting to create a push notification with Firebase.
I constantly get the following error:
B4X:
Compiling code.    Error
Error parsing manifest script:
Error parsing google-services.json:
Error parsing manifest script:
Error parsing json file. Make sure that the package name is correct.
My Package name is correct "LeadGuru.b4a" - OR IS IT :)
My package details are:
Firebase.png

What is the "Continue SDK setup"?
Firebase Settings:
settings.png

My google-services.json file:
B4X:
{
  "project_info": {
    "project_number": "879829432414",
    "firebase_url": "https://leadguru-cf738.firebaseio.com",
    "project_id": "leadguru-cf738",
    "storage_bucket": "leadguru-cf738.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:879829432414:android:56f04f809170e1be0d1ccf",
        "android_client_info": {
          "package_name": "com.rob.leadguru"
        }
      },
      "oauth_client": [
        {
          "client_id": "879829432414-noqgsjoq709ubgm28scmdc8sgkcvvs81.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyD1KsOKhUaVJgLgthYTJvWsaDHF8ViHiK4"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "879829432414-noqgsjoq709ubgm28scmdc8sgkcvvs81.apps.googleusercontent.com",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}
 

Declan

Well-Known Member
Licensed User
Longtime User
@aeric Many Thanks
My app compiles - but is an "empty" app just to set Firebase up correctly.
Are there example B4A apps available and also B4J Ui apps that will allow me to send/Receive notifications?
I must eventually be able to send notifications to users that meet one or two criteria.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Yes, that is the thread I am using for my app.
I keep getting the following error:
B4X:
Logger connected to:  Xiaomi MI 4W
--------- beginning of crash
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
Error occurred on line: 11 (FirebaseMessaging)
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.rob.leadguru. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
    at com.google.firebase.messaging.FirebaseMessaging.getInstance(com.google.firebase:firebase-messaging@@20.1.0:1)
    at anywheresoftware.b4a.objects.FirebaseNotificationsService$FirebaseMessageWrapper.Initialize(FirebaseNotificationsService.java:97)
    at com.rob.leadguru.firebasemessaging._service_create(firebasemessaging.java:172)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at com.rob.leadguru.firebasemessaging.onCreate(firebasemessaging.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
    at android.app.ActivityThread.access$2000(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1492)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
** Service (firebasemessaging) Start **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
** Service (firebasemessaging) Destroy **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
** Service (firebasemessaging) Destroy **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **

My "FirebaseMessaging" Service is:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

My B4J Code is:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private const API_KEY As String = "AIzaSyD1KsOKhUaVJgLgthYTJvWsaDHF8ViHiK4"
End Sub

Sub AppStart (Args() As String)
    SendMessage("general", "title", "body")
    StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub


Sub JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
   ExitApplication '!
End Sub

Any the B4J Log is:
B4X:
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Waiting for debugger to connect...
Program started.
ResponseError. Reason: , Response: <HTML>
<HEAD>
<TITLE>INVALID_KEY_TYPE</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>INVALID_KEY_TYPE</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
[jobname=fcm, success=false, username=
, password=, errormessage=<HTML>
<HEAD>
<TITLE>INVALID_KEY_TYPE</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>INVALID_KEY_TYPE</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@3bd40a57, response=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@fdefd3f
, tag=java.lang.Object@d83da2e, main=null, httputils2service=null
]
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Do you have the following in Manifest Editor?

B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private const API_KEY As String = "AIzaxxxxxxxxxxxxxxx"
End Sub
This was the problem
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
The notification is now received on my phone.
However, I do not get the notification shown on the app icon (red dot with number)
If I send multiple notifications, I should get the notification shown on the app icon - and number increment with each notification
 
Upvote 0
Top