B4A Library FirebaseNotifications - Push messages / Firebase Cloud Messaging (FCM)

Status
Not open for further replies.

aarroyo

Member
Licensed User
I have a problem , after initializing, do not assign me token and returns me the following error:

Error: Cannot run method from watch expression.
 

Attachments

Erel

Administrator
Staff member
Licensed User
This is not an error. It just means that the debugger cannot evaluate the expression.
 

alimanam3386

Active Member
Licensed User
Hi

As far as I understand this library ( FirebaseNotification ) is just for push a notify to users , but how can I send a message to an user ? ( without app_key , because it is secrect key and we can't use it in the source code ( client side ))

Regards
 

ilan

Expert
Licensed User
Hi

As far as I understand this library ( FirebaseNotification ) is just for push a notify to users , but how can I send a message to an user ? ( without app_key , because it is secrect key and we can't use it in the source code ( client side ))

Regards
what do you mean with message? SMS?

when you send the notify its up to you how to show it to the user you can show it as a notification or in another way. (like when user open the app show msgbox)
 

alimanam3386

Active Member
Licensed User
what do you mean with message? SMS?

when you send the notify its up to you how to show it to the user you can show it as a notification or in another way. (like when user open the app show msgbox)
Thank you, I want send a pm ( like whatsapp or telegram messangers ) userTouser pm , I know by this library I just can handel and get messages from server app. But how can I send pm without secret key ???
 

sasidhar

Active Member
Licensed User
hi,

I followed the procedure mentioned for firebug notifications on mobile. when I run Jar file i get error mentioned below. same project in the example i have used. API key changed, compiled the project, FCMPush.jar file created. same using command prompt when i run i get the error. please let me know the issue.

C:\Softwares\basic4android-Licensed\Projects\Samples\NFC\UltraLightReader\Limousine\Limousine b4x6.0\FCMPush\Objects>java -jar FCMPush.jar
Picked up _JAVA_OPTIONS: -Xmx1g -Xms1g
httpjob._postbytes (java line: 215)
java.lang.NoClassDefFoundError: javafx/application/Platform
at anywheresoftware.b4j.objects.FxBA.postRunnable(FxBA.java:17)
at anywheresoftware.b4a.keywords.Common.CallSubDelayed4(Common.java:500)
at anywheresoftware.b4a.keywords.Common.CallSubDelayed2(Common.java:484)
at b4j.example.httpjob._postbytes(httpjob.java:215)
at b4j.example.httpjob._poststring(httpjob.java:261)
at b4j.example.main._sendmessage(main.java:109)
at b4j.example.main._appstart(main.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
Caused by: java.lang.ClassNotFoundException: javafx.application.Platform
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang

thanks
sasidhar
 

Erel

Administrator
Staff member
Licensed User
Are you sure that you have used the B4J project as is? Looks like you have created a UI project instead of a non-ui project. Anyway you should use Java 8u40+.

For further discussion please start a new thread in the B4J questions forum.
 

Anser

Well-Known Member
Licensed User
Question : If i need to send a message for just one selected device out of 100 devices, how can I do that?

Answered by Erel : Either subscribe each device to a unique topic or use your own server with the token ids (see FirebaseMessaging.Token).
While using GCM, in the PushService Service we used to make use of the following event Sub to get the Token ID

B4X:
Sub HandleRegistrationResult(Intent As Intent)
    If Intent.HasExtra("error") Then
        Log("Error: " & Intent.GetExtra("error"))
        ToastMessageShow("Error: " & Intent.GetExtra("error"), True)
    Else If Intent.HasExtra("unregistered") Then
        Log("Reached HandleRegistrationResult Unregister")
        'Remove GCM ID details from My Table

    Else If Intent.HasExtra("registration_id") Then
        Log("Reached HandleRegistrationResult registration ID")
        Dim rid As String
        rid = Intent.GetExtra("registration_id")
        Log("GCM RegID from PushService : " & rid )
        'Update My Table with the newly received GCM Registration ID
     
    End If
End Sub
Here you have mentioned about the FirebaseMessaging.Token, Is there any even to get the TokenID like as shown above in case of Gcm
OR
The Token ID is available immediately after Subscribing to a topic
For eg:
B4X:
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
   fm.Token 'Will I get the Token ID from here
End Sub
Or Should I have some Sub in the FireBaseMessaging Service as follows
B4X:
Sub fm_TokenRefresh(cTokenID as String)
   Log( cTokenID )
End Sub
 

Erel

Administrator
Staff member
Licensed User
fm.Token will return the current token. TokenRefresh will fire whenever the token changes.
 

Anser

Well-Known Member
Licensed User
fm.Token will return the current token. TokenRefresh will fire whenever the token changes.
Thanks for confirming.

So, incase if the token changes, is that the right way to get the refreshed token ?. I mean the code in FireBaseMessaging Service as posted below
B4X:
Sub fm_TokenRefresh(cTokenID as String)
   Log( cTokenID )
End Sub
Do I have to take care of the above in FireBaseMessaging Service ?
 

Anser

Well-Known Member
Licensed User
I am just converting my existing code that used GCM to FCM

When we receive Push Messages In GCM, in the Sub MessageArrived (Intent As Intent), we used to check whether a particular property/data was there in the sent message using the following code

B4X:
Dim cMsg As String
Dim UseBigPictureStyle As Boolean
UseBigPictureStyle = False
If Intent.HasExtra("message") Then cMsg = Intent.GetExtra("message")
If Intent.HasExtra("UseBigPictureStyle") Then UseBigPictureStyle = True
How do I execute the same when I receive Message in FCM in the Sub fm_MessageArrived (Message As RemoteMessage)
The following works well if the received message contains a Key, but fails when the received message does not contain the key. For eg
B4X:
Dim cMsg As String
Dim UseBigPictureStyle As Boolean
UseBigPictureStyle = False
If Message.GetData.Get("message") <> "" Then cMsg = Message.GetData.Get("message")
'If  "UseBigPictureStyle" is not included then the code freezes there
If Message.GetData.Get("UseBigPictureStyle") <> "" Then UseBigPictureStyle = True
If I include UseBigPictureStyle data in the message that I send, then everything works fine, but if it doesn't contain then the code does not get executed beyond the point
If Message.GetData.Get("UseBigPictureStyle") <> "" Then UseBigPictureStyle = True


EDIT : Sorry for the confusion. Forgot that it is a Map, the solution is
B4X:
If Message.GetData.ContainsKey("UseBigPictureStyle") Then UseBigPictureStyle = True
Regards

Anser
 
Last edited:

alimanam3386

Active Member
Licensed User
Hi

When I use Web Socket Server(B4J) to send a message with FCM I get an error :

B4X:
{
  "data": {
  "receivertoken": "",
  "pmsenderid": "SenderId HERE",
  "body": "hi my friend !"
  },
  "to": "AcQQ4p4WI1g:APA91bGhKhiydgATbIkNgzu5F6WGF5pm_HxLM9lNSA7bHM0dg89sCbpgpEQc3srf4J92oZkyKd3DId5HqroBmWMWkGCVMHYvxOY1nYrcV6GnTJ--x2T61igugVdDwgCM-D-OJPzZThze"
}

java.lang.RuntimeException: java.lang.IllegalStateException: Toolkit not initialized
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
   at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
   at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
   at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
   at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:195)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
   at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
   at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
   at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
   at javafx.application.Platform.runLater(Platform.java:83)
   at anywheresoftware.b4j.objects.FxBA.postRunnable(FxBA.java:17)
   at anywheresoftware.b4a.keywords.Common.CallSubDelayed4(Common.java:500)
   at anywheresoftware.b4a.keywords.Common.CallSubDelayed2(Common.java:484)
   at b4j.example.httpjob._postbytes(httpjob.java:215)
   at b4j.example.httpjob._poststring(httpjob.java:261)
   at b4j.example.b4a._v6(b4a.java:123)
   at b4j.example.b4a._pm_message(b4a.java:79)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
   ... 10 more
I attached my source code , how can I solve this error ?
 

Attachments

Status
Not open for further replies.
Top