Hi,
I am trying to implement Facebook App Events using their Hybrid app implementation guide.
In the Android section of the page, it gives this one line code that we have to add in the native app codebase.
AppEventsLogger.augmentWebView(<YOUR_WEBVIEW_OBJECT>, <YOUR_ANDROID_CONTEXT>)
To be honest, I have no clue on where to put this piece of code at but I started trying out in the file MainActivity.java in the onCreate() method.
Code placement aside, I also have no clue on how to obtain the WEBVIEW_OBJECT and ANDROID_CONTEXT function arguments.
Some of things I have tried are:
Context context = ((Bridge) bridge).getContext();
WebView webView = ((Bridge) bridge).getWebView();
Those lines didn’t produce any build errors but the app crashed as soon as it launches.
The errors I got are:
java.lang.RuntimeException: Unable to start activity ComponentInfo{io.ionic.starter/io.ionic.starter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.Context com.getcapacitor.Bridge.getContext()’ on a null object reference
java.lang.RuntimeException: Unable to start activity ComponentInfo{io.ionic.starter/io.ionic.starter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.webkit.WebView com.getcapacitor.Bridge.getWebView()’ on a null object reference
I have been scratching my head over this roadblock for a few days now. Can anyone help? I am sure it is something simple for someone seasoned in Android development.
@shaifulborhan Not 100% sure but there is a pretty good chance you will have to create a custom plugin for what you want to achieve. What do you exactly want to do and why do you have to do it in the native side and not in the javascript side?
@armandovillalta Thank you for your reply. To answer your question, if I just use the standard JS method (FB Pixel) to track events, I won’t get the best possible FB tracking features like offline tracking, device information, app installations and launches data etc.
So apparently FB offers a way to enable native tracking in a hybrid app like Ionic by just adding “one line of code in the native codebase” according to their guide. On the JS side of things, you only have to add one line of code too on top of the standard FB Pixel event tracking codes.
The JS side is straightforward but the native side implementation is really driving my nuts @@
@shaifulborhan Got it well then I think you should take a look at this:
As I said there is a good chance you needed a plugin… this is a cordova plugin so you should use cordova in the ionic app instead of capacitor. If you want to continue working with capacitor I believe you should creatr a @ionic-native wrapper for this plugin so it can work properly with capacitor. Either way take a look at this… if you need more help let me know.
I was having this same issue, and I believe it’s because bridge
is not yet available. Use the @Override
as explained here to let your plugin know when it’s ready.
So:
@Override
public void load() {
// Do stuff here to access methods on the bridge
}