Search code examples
javaandroidauthenticationcallbackbackground-process

How to receive callback with app in background


I would like to be able to receive a callback when my app is in the background and then switch back to the application without any manual steps from the user when the callback is received. An activity opens an external browser for the user to perform some action. Then the phone should receive a callback; which it does, but not until the user manually switches back to the application from the webview (an undesired step). Until then, the page seems to get hung up. My questions are...

  1. Do I need to create a websocket? or Notification Handler?
  2. If so, how would I go about that for this case?
  3. Do I need to switch this to an activityforResult and when the result (any result) happens to then switch back to the application?
  4. if so, how would I do that?

Here is what I have in terms of code: Open the web page where 'exe' is the webpage as a string

Log.i("inititating commands for", exe);
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(exe));
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setPackage("com.android.chrome");
                try {
                    context.startActivity(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }

mainfest.xml intent filters (i'm not certain this is neccessary, nor am I sure it changed anything for me)

<intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                           <action android:name="android.intent.action.VIEW" />
                           <category android:name="android.intent.category.DEFAULT"/>
                           <category android:name="android.intent.category.BROWSABLE" />
                           <data android:host="127.0.0.1:54321/" android:scheme="http"/>
            </intent-filter>

I'm experiencing quasi-success. The flow completes, but with extra steps from the user. I would like to remove those manual steps, and make the flow seamless from the user's perspective.


Solution

  • define your custom schema on intent filter

        <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="your_custom_scheme" />
    </intent-filter>
    

    define a callback url

    String callbackUrl = "your_custom_scheme://callback";
    

    pass this to your intent as below

    intent.putExtra("android.intent.extra.REFERRER", Uri.parse(callbackUrl));
    

    When the user performs the desired action in the external browser and triggers the callback, the browser should attempt to open the callback URL. This should bring your app back to the foreground. In your activity, override the onNewIntent method to handle the callback when your app is already running

        @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Uri data = intent.getData();
        if (data != null && data.getScheme().equals("your_custom_scheme")) {
            // Handle the callback here
        }
    }