davco98
davco98

Reputation: 31

Open URL from Firebase Notification on WebView Activity

I just setup firebase notifications and I was wondering how th would I be able to open the url metadata contained in the notification so that it would be able to be opened inside the app's main webview

This is the WebView Activity:

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)
    }

This is the Notification Handler

class NotificationHandler: FirebaseMessagingService() {

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

Very straightforward...

And finally my notification is setup in this way, and sent via Pusher from another Server (a PHP based server)

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

I have really no idea how to transport data from one side to another, (I already did it with iOS doing exactly what i want, which is: Whenever I receive a notification, and i open it, an alert is shown on screen inside the app, with two options... Open it (which opens the URL inside the WebView) and Ignore (Which makes the webview continue on the default url)

Upvotes: 0

Views: 708

Answers (1)

SuuSoJeat
SuuSoJeat

Reputation: 1136

One way to pass the data is through FirebaseJobDispatcher. You basically serialized your notification data and put it in the bundle. Here's a sample app from Google Firebase

Upvotes: 1

Related Questions