Friday, 8 October 2021

How to launch android app only when onesignal device id is ready?

I am trying to implement webview using onesignal for notifications. I need to pass onesignal device ID to the server using the insertion of JavaScript code to the page of my webview (I assign to a LOGIN button a JS function that sends device ID to the server). To do this I use the following code:

public void onPageFinished(WebView view, String url) {

// get device id
                OSDeviceState deviceState = OneSignal.getDeviceState();
                String oneSignalDeviceID = deviceState != null ? deviceState.getUserId() : null;

// create string with oneSignalDeviceID 
                String jsCodeInsert =
                        "javascript:document.getElementById('wp-submit').onclick = function() { jQuery.post( ajax_object.ajax_url, {action: 'onesignal_add', id: '" + oneSignalDeviceID + "', mode: 'mymode'}, function( response ) {console.log(response);});} ";

                     view.loadUrl(jsCodeInsert);
            }

Problem
Often oneSignalDeviceID contains null value instead correct ID. As far as I understand it, when it contains null, onesignal has not created a ID for a user.

What is the simplest solution in this case? I think the easiest thing to do is not to launch the webview until oneSignalDeviceID is ready (is not null), however, I am not sure how to do this.



from How to launch android app only when onesignal device id is ready?

No comments:

Post a Comment