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?
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
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
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?
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";
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";