WebView活动上来自Firebase通知的打开URL

时间:2019-01-24 07:31:48

标签: android kotlin

我只是设置了Firebase通知,我想知道如何才能打开通知中包含的url元数据,以便能够在应用程序的主Web视图中打开

这是WebView活动:

class WebViewActivity : AppCompatActivity() {

    private val webURL = "domain"
    private val isAlreadyCreated = false
    private var webViewFailed = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)

        PushNotifications.start(getApplicationContext(), "CODE")

        setContentView(R.layout.activity_web_view)

        webView.settings.javaScriptEnabled = true
        webView.settings.setSupportZoom(false)
        webView.settings.setAppCacheEnabled(true)

        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                if(webViewFailed == false){
                    loader.alpha = 0F
                    webView.alpha = 1F
                    img.alpha = 0F
                    loadingIndicator.alpha = 0F

                    var response = makeRequest("https://domain/api/verifySession")

                    val storedActiveId = getSharedPreferences("ActiveId",0)

                    if(storedActiveId.contains("ActiveId")&&storedActiveId.contains("DisabledId")){
                        if(response.toString() == "null") {
                            PushNotifications.subscribe("loggedOutUser")
                        }else if(storedActiveId.getString("ActiveId","0") != response.toString() && storedActiveId.getString("DisabledId","0") != response.toString()){
                            showSessionDialog("Hay una nueva cuenta",
                                    "¿Activamos notificaciones?", this@WebViewActivity, response.toString())
                        }
                    }else{
                        val editor = storedActiveId.edit()
                        editor.putString("ActiveId", response.toString())
                        editor.putString("DisabledId", "0")

                        // Commit the edits!
                        editor.commit()
                    }
                }
            }


            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                val url: String = request?.url.toString()
                if (url.contains("domain")) {
                    loadingIndicator.alpha = 1F
                    webView?.loadUrl(url)
                } else {
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse(url)
                    startActivity(i)
                }
                return true
            }

            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                super.onReceivedError(view, request, error)
                showErrorDialog("Error", "No internet connection",this@WebViewActivity)
                webViewFailed = true;
            }
        }

        webView.loadUrl(webURL)
    }

这是通知处理程序

class NotificationHandler: FirebaseMessagingService() {

    override fun onMessageReceived(mensaje: RemoteMessage) {
        super.onMessageReceived(mensaje)
    }
}

非常简单...

最后,我的通知是以这种方式设置的,并通过Pusher从另一台服务器(基于PHP的服务器)发送出去

            "fcm" => array(
              "notification" => array(
                "title" => $value->title,
                "body" => $value->message,
                "sound" => $sound,
                "url" => $value->url,
                "prompt" => true
              )

我真的不知道如何将数据从一侧传输到另一侧,(我已经使用iOS完全按照我想要的方式进行了传输,这是:每当我收到通知并打开它时,都会在屏幕上显示警报在应用程序内部的屏幕上,有两个选项...将其打开(将在WebView内打开URL)并忽略(这将使Webview继续使用默认URL)

1 个答案:

答案 0 :(得分:1)

一种传递数据的方法是通过FirebaseJobDispatcher。您基本上已经序列化了通知数据,并将其放入包中。 Here's a sample app from Google Firebase