ElonMuskofBadIdeas
ElonMuskofBadIdeas

Reputation: 27

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.

Upvotes: 1

Views: 509

Answers (1)

user16930239
user16930239

Reputation: 9717

Unfortunately, it not always a matter of time. Sometimes it will need the app to start second time for OneSignal to obtain player ID. And some users will disable notifications manually. In our apps we keep checking for OneSignal User ID until we get it, and we have updateOneSignalID API. usually it will not take more than the second run of the app.

Upvotes: 1

Related Questions