OneSignal - 帮我在现有的 webView 中打开 URL ...我很困惑

OneSignal - help me open URL within existing webView ... I'm so confused

我让某人为我创建了一个非常基本的 WebView 应用程序,其中包含 OneSignal 通知。该应用程序启动时会出现启动画面,然后主页上有两个按钮,将用户发送到不同的 URL - 在我下面的示例中,Google 和 Stack Overflow。

它工作正常,但我正在尝试进行更改以在点击通知时打开特定的 URL,但无法锻炼如何,尽管在这里阅读了大量内容和 OneSignal 文档。作为最后的手段,我想我会 post 我的代码,看看是否有人可以帮助这个新手进行应用程序编码!

基本上,我需要修复 ExampleNotificationOpenedHandler class 最底部的启动 URL 代码。我通过 OneSignal 发送 URL 作为附加数据,应用程序在 launchURL 变量中正确读取它(当我查看 logcat 时)。

public class App extends Activity {


    private static final String WEB_URL1 = "https://google.com";
    private static final String WEB_URL2 = "https://whosebug.com";

    View ll_Web;
    WebView webView;
    SwipeRefreshLayout mSwipeRefreshLayout;
    RelativeLayout splash, home;
    View ll_pView, pView;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client2;


    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();

        setContentView(R.layout.main);

        ll_Web = findViewById(R.id.ll_Web);
        webView = (WebView) findViewById(R.id.webView);
        splash = (RelativeLayout) findViewById(R.id.splash);
        home = (RelativeLayout) findViewById(R.id.home);

        ll_pView = (View) findViewById(R.id.ll_pView);
        pView = (View) findViewById(R.id.pView);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pView.getLayoutParams());
                lp.weight = progress;
                pView.setLayoutParams(lp);

                ll_pView.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                mSwipeRefreshLayout.setRefreshing(false);
                ll_pView.setVisibility(View.GONE);

                super.onPageFinished(view, url);

            }
        });
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });


        findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                webView.clearHistory();
                webView.loadUrl(WEB_URL1);
                home.setVisibility(View.GONE);
                ll_Web.setVisibility(View.VISIBLE);
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                webView.clearHistory();
                webView.loadUrl(WEB_URL2);
                home.setVisibility(View.GONE);
                ll_Web.setVisibility(View.VISIBLE);
            }
        });

        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                home.setVisibility(View.VISIBLE);
                splash.setVisibility(View.GONE);
            }
        }, 3 * 1000);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.

    }

    @Override
    public void onBackPressed() {
        if (ll_Web.getVisibility() == View.VISIBLE) {

            if (webView.canGoBack() && !webView.getUrl().equals(WEB_URL1) && !webView.getUrl().equals(WEB_URL2)) {
                webView.goBack();
            } else {
                home.setVisibility(View.VISIBLE);

                if (Build.VERSION.SDK_INT < 18) {
                    webView.clearView();
                } else {
                    webView.loadUrl("about:blank");
                }
                ll_Web.setVisibility(View.GONE);
            }
        } else
            super.onBackPressed();

    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */


    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        //client2.connect();

    }

    @Override
    public void onStop() {
        super.onStop();


    }



class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        // This fires when a notification is opened by tapping on it.

        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
            OSNotificationAction.ActionType actionType = result.action.type;
            JSONObject data = result.notification.payload.additionalData;
            String launchURL;

            if (data != null) {
                launchURL = data.optString("launchURL");
                if (launchURL != null) {
                    Log.i("OneSignalExample", "launchURL value: " + launchURL);


           //        How do I send the LaunchURL to open webview???
           //        webView.loadUrl(launchURL);


                }


            }

        }
    }


}

我没有编写此代码,但如有任何帮助,我们将不胜感激。

像你一样使用按钮

    if (launchURL != null) {
                Log.i("OneSignalExample", "launchURL value: " + launchURL);


                webView.clearHistory();
                    webView.loadUrl(launchURL);
                    home.setVisibility(View.GONE);
                    ll_Web.setVisibility(View.VISIBLE); 


            }

我认为设置 url 时不会创建 webview,如果可以的话,在 onStart 中添加动作侦听器

    @Override
    protected void onStart()
   {
        super.onStart();
     OneSignal.startInit(this)
            .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
            .init();

       }

     }

或至少在 setContentView(R.layout.main); 之后 喜欢这里

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          OneSignal.startInit(this)
                .setNotificationOpenedHandler(new 
                   ExampleNotificationOpenedHandler())
                      .init();

为了其他搜索类似问题的人的利益,我的工作代码如下。作为新手,我不确定它是否是最好的解决方案,但它对我一直在做的测试很有效。

再次感谢@nimi_moradi 帮助我解决问题的建议。

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
    // This fires when a notification is opened by tapping on it and
    // puts the launchURL additional data into a string for use in the main activity
    public static String launchURL;

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;
        //public String launchURL;

        if (data != null) {
            launchURL = data.optString("launchURL");
            if (launchURL != null) {
              //  Log.i("OneSignalExample", "launchURL value: " + launchURL);
            }
        }

    }
}



public class App extends Activity {


    private static final String WEB_URL1 = "https://google.com";
    private static final String WEB_URL2 = "https://whosebug.com";

    View ll_Web;
    WebView webView;
    SwipeRefreshLayout mSwipeRefreshLayout;
    RelativeLayout splash, home;
    View ll_pView, pView;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client2;


    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();

        setContentView(R.layout.main);

        ll_Web = findViewById(R.id.ll_Web);
        webView = (WebView) findViewById(R.id.webView);
        splash = (RelativeLayout) findViewById(R.id.splash);
        home = (RelativeLayout) findViewById(R.id.home);

        ll_pView = findViewById(R.id.ll_pView);
        pView = findViewById(R.id.pView);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pView.getLayoutParams());
                lp.weight = progress;
                pView.setLayoutParams(lp);

                ll_pView.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                mSwipeRefreshLayout.setRefreshing(false);
                ll_pView.setVisibility(View.GONE);

                super.onPageFinished(view, url);

            }
        });
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });


        //Splash screen and main menu (unless launchURL exists)
        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                if ("".equals(ExampleNotificationOpenedHandler.launchURL) || ExampleNotificationOpenedHandler.launchURL==null){
                    // Show home screen with menu buttons
                    home.setVisibility(View.VISIBLE);
                }
                // Show splash screen for three seconds
                splash.setVisibility(View.GONE);
            }
        }, 3 * 1000);


        // if launchURL contains data, open that
        if (!"".equals(ExampleNotificationOpenedHandler.launchURL)) {
            home.setVisibility(View.GONE);
            ll_Web.setVisibility(View.VISIBLE);
            webView.clearHistory();
            webView.loadUrl(ExampleNotificationOpenedHandler.launchURL);

        }

        // Menu button one tapped - Google
        findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                webView.clearHistory();
                webView.loadUrl(WEB_URL1);
                home.setVisibility(View.GONE);
                ll_Web.setVisibility(View.VISIBLE);
            }
        });

        // Menu button two tapped - Stack Overflow
        findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                webView.clearHistory();
                webView.loadUrl(WEB_URL2);
                home.setVisibility(View.GONE);
                ll_Web.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (ll_Web.getVisibility() == View.VISIBLE) {

            if (webView.canGoBack() && !webView.getUrl().equals(WEB_URL1) && !webView.getUrl().equals(WEB_URL2)) {
                webView.goBack();
            } else {
                home.setVisibility(View.VISIBLE);

                if (Build.VERSION.SDK_INT < 18) {
                    webView.clearView();
                } else {
                    webView.loadUrl("about:blank");
                }
                ll_Web.setVisibility(View.GONE);
            }
        } else
            super.onBackPressed();

    }


    @Override
    public void onStart() {
        super.onStart();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        //client2.connect();

    }


    @Override
    public void onStop() {
        super.onStop();


    }



}