0

I have public class MyFirebaseInstanceIdService to handle firebase notifications when the app is in the background.

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        MainActivity.sendRegToken();
    }
}

MainActivity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    WebView webView = (WebView) findViewById(R.id.webView);

    public void sendRegToken() {
        String recent_token = FirebaseInstanceId.getInstance().getToken();
        webView.loadUrl("javascript:sendRegToken('"+recent_token+"');");
    }
}

I need access to webView so I can call loadUrl. sendRegToken() is a non-static method, and I'm trying to access it from a static context. I can't make sendRegToken() static because it contains the non-static webView. How do I get around this problem?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Bazley
  • 2,699
  • 5
  • 36
  • 61
  • 1
    Do you really need a `WebView` to send the token? A `WebView` is mainly a piece of UI, tied to an `Activity`. Even if there are ways to use it as a non-UI element, probably is not the best approach, and probably implementing the code to send the token as native would be a better solution. – Xavier Rubio Jansana Aug 15 '17 at 15:25

3 Answers3

0

Use class LocalBroadcastManager Example can be found here : LocalBroadcastManager Example 1.register broadcast on the MainActivity. 2.Send broadcast from onTokenRefesh() with parameters you need inside the Intent send.

Itzik Samara
  • 2,278
  • 1
  • 14
  • 18
0

Please, do not do that.

If you have an API for this communication, just use normal Http Requests, if you don't have an API see this

By the way, Greenrobot/EventBus way better and simpler than LocalBroadCastManager.

hcknl
  • 1,219
  • 10
  • 15
0

Disclaimer: Haven't tried, but in theory it should work...

Use an Observer pattern.

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    List<TokenRefreshListener> tokenListeners = new ArrayList<>();

    public void addTokenListener(TokenRefreshListener listener) {
       tokenListeners.add(listener);
   } 

  // Should also add a remove method

    @Override
    public void onTokenRefresh() {
        String token = getToken();
        for (TokenRefreshListener l:tokenListeners) {
            l.onTokenRefresh(token);
        } 
    }
}

And define that interface

public interface TokenRefreshListener {
    void onTokenRefresh(String token);
} 

And in the Activity, you need to implement that interface

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, 
    TokenRefreshListener {

        WebView webView = (WebView) findViewById(R.id.webView);

     // need to call addTokenListener of the service 
     //... Probably do that in onStart method

     @Override
    public void onTokenRefresh(String token) {
        webView.loadUrl("javascript:sendRegToken('"+token+"');");
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245