Android Question Firebase Push Notification - integration with ASP.NET

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all.

I want to integrate the Firebase Push Notification with my asp,net website. But from what I read so far is that the token generated by B4J GetTokenValue is valid for 1 hour only.
So if I start B4J sending tool and copy the token string from there and use it in my asp.net this token will expire in 1 hour.

Is there any options to either convert GetTokenValue to vb.net /c# or to pass some parameters into B4J project?
 

DonManfred

Expert
Licensed User
Longtime User
or to pass some parameters into B4J project?

The given parameters are in Args.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
...
End sub

I can´t help you with code in vb.net /c# to start an app and give parameters.

In Delphi i use

B4X:
    OpenProgram2('"C:\Java\jdk-11.0.1\bin\javaw.exe"',tempdir,'--module-path C:\Java\jdk-11.0.1\javafx\lib --add-modules ALL-MODULE-PATH -jar \\192.168.2.252\jars\UnternehmerErklaerung.jar '+IntToStr(abnr)+'');
where IntToStr(abnr) is ust the string-interpretion of abnr (int).

You can add more parameters.

OpenProgram2 is just a helper-sub around ShellExecute

B4X:
procedure OpenProgram2(prog, path, params: string);
var
  c, p, dir: array[0..800] of Char;
begin
  StrPCopy(c, prog);
  StrPCopy(p, params);
  StrPCopy(dir, path);
  ShellExecute(0, 'open', c, p, dir, SW_NORMAL);
end;
 
Last edited:
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
The given parameters are in Args.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
...
End sub

I can´t help you with code in vb.net /c# to start an app and give parameters.

In Delphi i use

B4X:
    OpenProgram2('"C:\Java\jdk-11.0.1\bin\javaw.exe"',tempdir,'--module-path C:\Java\jdk-11.0.1\javafx\lib --add-modules ALL-MODULE-PATH -jar \\192.168.2.252\jars\UnternehmerErklaerung.jar '+IntToStr(abnr)+'');
where IntToStr(abnr) is ust the string-interpretion of abnr (int).

You can add more parameters.

OpenProgram2 is just a helper-sub around ShellExecute

B4X:
procedure OpenProgram2(prog, path, params: string);
var
  c, p, dir: array[0..800] of Char;
begin
  StrPCopy(c, prog);
  StrPCopy(p, params);
  StrPCopy(dir, path);
  ShellExecute(0, 'open', c, p, dir, SW_NORMAL);
end;
Thanks, That's what I need.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
What exactly do you mean?
I'm asking - is it possible to convert into C# or VB/NET this code

B4X:
Private Sub GetTokenValue (FilePath As String) As String
    Dim GoogleCredentials As JavaObject
    GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
    Dim Credentials As JavaObject = GoogleCredentials.RunMethodJO("fromStream", Array(File.OpenInput(FilePath, ""))) _
        .RunMethod("createScoped", Array(Array As String("https://www.googleapis.com/auth/firebase.messaging")))
    Credentials.RunMethod("refreshIfExpired", Null)
    Return Credentials.RunMethodJO("getAccessToken", Null).RunMethod("getTokenValue", Null)
End Sub

I need to send messages from my asp.net website.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
I guess that it is possible. You need to go over Google documentation and find whether they provide asp.net SDK and follow their instructions.
Thank you. I will check it, But what if they don't have asp.net SDK?
 
Upvote 0

tznikos

Member
Licensed User
Longtime User
Hi, when run the code with the function GetTokenValue, getting the following error

B4X:
    GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
java.lang.ClassNotFoundException: com.google$auth$oauth2$GoogleCredentials
Running from B4J
Any idea why?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi, when run the code with the function GetTokenValue, getting the following error
You are posting in the WRONG thread
Posting to an existing thread is a mistake. Especially when the thema is a totally other one.

You should ALWAYS create a new Thread for your Issue.

PD: You probably forget to implement

B4X:
#AdditionalJar: google-auth-library-oauth2-http-1.18.0.jar
#AdditionalJar: google-auth-library-credentials-1.18.0.jar
#AdditionalJar: guava-23.0.jar
#AdditionalJar: google-http-client-1.43.3.jar
#AdditionalJar: google-http-client-gson-1.43.3.jar
#AdditionalJar: gson-2.10.1.jar
#AdditionalJar: opencensus-api-0.31.1.jar
#AdditionalJar: opencensus-contrib-http-util-0.31.1.jar
#AdditionalJar: grpc-context-1.27.2.jar
#PackagerProperty: AdditionalModuleInfoString = uses com.google.auth.http.HttpTransportFactory;

in your project.
 
Last edited:
Upvote 0

tznikos

Member
Licensed User
Longtime User
Alex here is your solution

B4X:
Imports System.IO
Imports Google.Apis.Auth.OAuth2
Class Firebase

    Public Function GetToken() As String
        Dim fileName As String = "your file.json"
        Dim scopes As String = "https://www.googleapis.com/auth/firebase.messaging"
        Dim bearertoken As String
        Using stream As New FileStream(fileName, FileMode.Open, FileAccess.Read)
            Dim credential As GoogleCredential = GoogleCredential.FromStream(stream).CreateScoped(scopes)
            bearertoken = credential.UnderlyingCredential.GetAccessTokenForRequestAsync().Result
        End Using
        Return bearertoken
    End Function
End Class

Find from Nuget the library Google.Apis.Auth.OAuth2
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thank you. Good idea.

Alternatively you can create a B4J web app including the B4J sending tool and post a json to the web app in a handler.
 
Upvote 0

ddefrain

Member
Licensed User
Longtime User
Hi all.

I want to integrate the Firebase Push Notification with my asp,net website. But from what I read so far is that the token generated by B4J GetTokenValue is valid for 1 hour only.
So if I start B4J sending tool and copy the token string from there and use it in my asp.net this token will expire in 1 hour.

Is there any options to either convert GetTokenValue to vb.net /c# or to pass some parameters into B4J project?
Firebase Notifications C#
There you go @Alex_197

Note: If you handle a topic you don't need to use/send the FCM Token id; I mean send the topic instead of.
And Yes Google Do that for security reasons the bearertoken lasts only one hour that's why you need to create a new one

Extract of modified code

B4X:
public class Message {

  public string token {
    get;
    set;
  }

  public Data data {
    get;
    set;
  }

  public Notification notification {
    get;
    set;
  }

  public string topic {
    get;
    set;
  }



}

B4X:
Root rootObj = new Root();
  rootObj.message = new Message();

  //rootObj.message.token = "cEM68BIdTomaE0R2dbaO:APA91bG8XfOjU_GSPNQYCrJ4wzE7VmMPEsyudwtE41VWKzJcoT2f3wbKsKCHwk5s078ZL31mM258-BzdZPRNXAlc_fyzCzj2txLQvQ3u7jggDPHjYIMlHRgspXT0CudfK"; //FCM Token id
 
  rootObj.message.data = new Data();
  rootObj.message.data.title = "Data Title";
  rootObj.message.data.body = "Data Body";
  rootObj.message.data.key_1 = "Sample Key";
  rootObj.message.data.key_2 = "Sample Key2";
  rootObj.message.notification = new Notification();
  rootObj.message.notification.title = "Notify Title";
  rootObj.message.notification.body = "Notify Body";
  rootObj.message.topic = "general";

Hope it helps!
 
Upvote 0

ddefrain

Member
Licensed User
Longtime User
Firebase Notifications C#
There you go @Alex_197

Note: If you handle a topic you don't need to use/send the FCM Token id; I mean send the topic instead of.
And Yes Google Do that for security reasons the bearertoken lasts only one hour that's why you need to create a new one

Extract of modified code

B4X:
public class Message {

  public string token {
    get;
    set;
  }

  public Data data {
    get;
    set;
  }

  public Notification notification {
    get;
    set;
  }

  public string topic {
    get;
    set;
  }



}

B4X:
Root rootObj = new Root();
  rootObj.message = new Message();

  //rootObj.message.token = "cEM68BIdTomaE0R2dbaO:APA91bG8XfOjU_GSPNQYCrJ4wzE7VmMPEsyudwtE41VWKzJcoT2f3wbKsKCHwk5s078ZL31mM258-BzdZPRNXAlc_fyzCzj2txLQvQ3u7jggDPHjYIMlHRgspXT0CudfK"; //FCM Token id
 
  rootObj.message.data = new Data();
  rootObj.message.data.title = "Data Title";
  rootObj.message.data.body = "Data Body";
  rootObj.message.data.key_1 = "Sample Key";
  rootObj.message.data.key_2 = "Sample Key2";
  rootObj.message.notification = new Notification();
  rootObj.message.notification.title = "Notify Title";
  rootObj.message.notification.body = "Notify Body";
  rootObj.message.topic = "general";

Hope it helps!
it will return a response like this
1718753182781.png
 
Last edited:
Upvote 0
Top