3

So, I can't afford a codeameone pro account, so I implemented native push notifications for Android with GCM. After some struggle I had it working.

Now GCM is deprecated and I want to migrate to FCM. However, I ran into some issues because codename one doesn't allow me to inject the right dependencies into the build.gradle file in order for the firebase app to initialize properly.

I described those here:

https://stackoverflow.com/questions/52278220/codenameone-firebaseapp-not-initializing

Also tried using some of the functionality for th pro account in order to add the right fcm dependencies to my app, meaning having my main applicatoin class implement the PushCallback interface, but I ran into other problems. For example, codenameone overrides my FirebaseMessagingService implementation, with a proprietary one, which throws an error when I'm sending a custom notification payload, via google fcm endpoint, which previously worked with my native implementation.

The error I get is this one:

09-12 14:50:19.581 14378 14397 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at java.io.DataOutputStream.writeUTF(DataOutputStream.java:347)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at java.io.DataOutputStream.writeUTF(DataOutputStream.java:323)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at com.codename1.impl.android.AndroidImplementation.appendNotification(AndroidImplementation.java:470)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at com.codename1.impl.android.CN1FirebaseMessagingService.onMessageReceived(CN1FirebaseMessagingService.java:83)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at com.google.firebase.messaging.FirebaseMessagingService.zzc(Unknown Source)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at com.google.firebase.iid.zzc.run(Unknown Source)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
09-12 14:50:19.581 14378 14397 E AndroidRuntime:        at java.lang.Thread.run(Thread.java:761)

Apparently the CN1FirebaseMessagingService implementation expects a "body" parameter on the notification.

So right now I'm stuck, either not being able to add the right fcm dependencies to the build or not being able to implement my on FirebaseMessagingService.

My question is: Is there a way to add fcm support to my app without having a pro account and having to use codenameone servers to relay the push ?

I would prefer to use the fcm endpoing to send push notifications from my server and add custom notification handling on the device.

Thanks.

Adrian Ionescu
  • 441
  • 2
  • 8

1 Answers1

1

Eventually I managed to implement FCM support natively by ading all the required dependencies via android.gradleDep build hint and initializing FirebaseApp manually, as described here: Can I initialize Firebase without using google-services.json?

Adrian Ionescu
  • 441
  • 2
  • 8
  • Hi Adrian can you please share your working code. I also need to implement push...or a blog post where you detailed the implementation process. email is lukman at lukmanjaji dot com. thanks in advance – javalove Oct 21 '18 at 05:47
  • Hi, I don't have time to detail the implementation, but there's nothing special to it. Just use some native code to implement android notifications support as for any Android apllication. See fcm documentation: https://firebase.google.com/docs/cloud-messaging/android/client . Then use android.xapplication cn1 flag to inject manifest confituration. And finally, use a cn1 native interface to initialize the fcm service as described in the link from the accepted answer. – Adrian Ionescu Oct 22 '18 at 06:32