求助:如何在Urban Airship中重写OpenExternalUrlAction行为并调整应用流程
Got it, let's tackle this Urban Airship redirect flow issue you're facing. Here's how you can take control of the external web page action and adjust the launch sequence to keep the web page in the foreground:
To gain control over how the external web page is opened, you'll need to replace Urban Airship's default web action with a custom one. This lets you bypass the SDK's default behavior and handle the redirect exactly as you need.
- Create a custom
Actionclass that extends Urban Airship'sWebViewAction(or implements theActioninterface for full control). This class will dictate how the web page is launched. - Register your custom action during Airship initialization using the
ActionRegistryto replace the default web action:
ActionRegistry actionRegistry = UAirship.shared().getActionRegistry(); actionRegistry.replaceAction(WebViewAction.DEFAULT_REGISTRY_NAME, new CustomWebPageAction());
The core problem is your Home Activity's heavy background tasks pushing the web page to the background. You'll need to prioritize the web page launch before running those tasks.
- In your Home Activity's
onCreatemethod, first check if the incoming intent contains an Urban Airship web page payload. - If a web page is pending, launch it immediately, then delay your heavy initialization tasks until later. Example code:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); Intent incomingIntent = getIntent(); if (incomingIntent != null && incomingIntent.hasExtra(WebViewAction.EXTRA_URL)) { // Launch the web page first String targetUrl = incomingIntent.getStringExtra(WebViewAction.EXTRA_URL); launchWebPageActivity(targetUrl); // Delay heavy tasks to avoid pushing the web page to background new Handler(Looper.getMainLooper()).postDelayed(() -> { runHomeActivityHeavyTasks(); }, 1500); // Adjust delay based on your task's runtime } else { // Normal launch flow for when there's no web notification runHomeActivityHeavyTasks(); } } private void launchWebPageActivity(String url) { Intent webIntent = new Intent(this, CustomWebActivity.class); webIntent.putExtra("WEB_URL", url); startActivity(webIntent); }
Ensure your custom web activity has the right launch mode and task settings to avoid being pushed to the background:
Add these attributes to your web activity in AndroidManifest.xml:
<activity android:name=".CustomWebActivity" android:launchMode="singleTop" android:taskAffinity="" android:excludeFromRecents="true"> </activity> - `taskAffinity=""` creates a separate task stack for the web activity, so it won't be affected by your Home Activity's task operations. - `singleTop` prevents duplicate instances of the web activity from opening.
Make sure your Urban Airship notification payload triggers your custom action instead of the default one. Adjust the payload like this:
{ "notification": { "alert": "Check out this update!" }, "actions": { "custom_web_action": { "url": "https://your-target-url.com" } } }
Replace custom_web_action with the registry name you used for your custom action.
内容的提问来源于stack exchange,提问作者Ravind Maurya

