简体   繁体   中英

OneSignal PlayerId from android studio webview app

There's been a lot of talk about this already, but I just found confusion.

I want to send notifications on my android app to a single user.

(I hate android studio, and I'm not that good at java)

I have my website with registration/login, and user id.

The idea is to create a mysql table with my Onesignal userId and playerId, and then send push notifications to a single user via curl. 在此处输入图像描述

I connected oneSignal to my app in android studio. 在此处输入图像描述

(I receive push notification if i send it from onesignal)

I have this code which allows me to save the playerId of OneSignal in my database, but I can only get the playerId from website, not from android app.

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
    <script>
      var OneSignal = window.OneSignal || [];
      OneSignal.push(function() {
        OneSignal.init({
          appId: "bb1bf59b-fb3b-4906-a0a8-279d5623f9d7"
        });
      });
      
  OneSignal.push(function() {
  
  //playerid salvato in mysql
  OneSignal.isPushNotificationsEnabled(function(isEnabled) {
  if (isEnabled) {
      // user has subscribed
      OneSignal.getUserId( function(userId) {
          //console.log('player_id of the subscribed user is : ' + userId);
          // Make a POST call to your server with the user ID  
          AjaxOneSignal('onesignal.php', '?uid=<? echo $user_uid; ?>&rand=<? echo time(); ?>&playerid='+userId);      
      });
  }
});
});
</script>

How do I get it from android app?

(Sending my userId as a tag to OneSignal would also be fine for me, I just need a quick and easy way that works.

Please help me get out of this stupid nightmare...

(No, i dont want to use firebase, that thing doesn't work, it breaks with every update of a plugin, a module, an sdk, android studio, etc, etc, etc... I wasted three days of my life with that crap without getting anything.)

Updated:

As I see you have OneSignal SDK in your application. You can use this method to get playerId

OneSignal.addSubscriptionObserver(new OSSubscriptionObserver() {
    @Override
    public void onOSSubscriptionChanged(OSSubscriptionStateChanges osSubscriptionStateChanges) {
       String playerId = osSubscriptionStateChanges.getFrom().getUserId();
    }
}); 

Here can use JavascriptInterface , it allows to call java code from the javascript.

  1. When you get userId from OneSignal store it in some variable.

  2. Then create method like this. In this case I create it in Activity:

     @JavascriptInterface public String getUserId() { return userId; }
  3. After that you need add JavaScriptInterface to your webview:

     webView.addJavascriptInterface(this,"OneSignalBridge") webView.setWebChromeClient(new WebChromeClient());

Here we use this , because our method with JavascriptInterface annotation located in Activity.

OneSignalBridge - is just a random name, you can use any name you want

  1. Now you can call this method from javascript code.

     <script> function someFunc(){ var userId = OneSignalBridge.getUserId(); } </script>

So... i do this在此处输入图像描述

And this在此处输入图像描述

but no id from android app and i see this error in firefox在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM