WebView with push notification url from mainactivity

Send notification of incoming URLs to the page with the main activity of the recipient on the WebView page, and I want to open the URL on this page.

But he did the current project, directing the browser, opening the page in the receiver. I would be grateful if you could help.

JSON data that I send via push:

{
  "alert": "Push bildirimi",
  "badge": "Increment",
  "uri": "http://www.google.com.tr"
}

Receiver.java

import android.app.Activity;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import com.parse.ParseAnalytics;
import com.parse.ParsePushBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;

public class Receiver extends ParsePushBroadcastReceiver {


  // public MainActivity anasayfa;


    /*public Receiver(WebView webview) {
        this.webview = webview;
    }*/

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        super.onPushOpen(context, intent);
        ParseAnalytics.trackAppOpenedInBackground(intent);
        // Anlik Bildirim acildiginda MainActivity'imizi baslatiyoruz.

        Log.d("msg_info", "onPushOpen");
        String uriString = null;
        try {
            Log.d("msg_info", "icerdeyim");

            JSONObject pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
            uriString = pushData.optString("uri");
        } catch (JSONException e) {
            Log.v("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", e);
        }
        Class<? extends Activity> cls = getActivity(context, intent);
        Intent activityIntent;
        if (uriString != null && !uriString.isEmpty()) {
            activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
        } else {
            activityIntent = new Intent(context, MainActivity.class);
        }
        activityIntent.putExtras(intent.getExtras());
        if (Build.VERSION.SDK_INT >= 22) {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(cls);
            stackBuilder.addNextIntent(activityIntent);
            stackBuilder.startActivities();
        } else {
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(activityIntent);
        }
    }
}

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.computer.bildirimler.R;

/*import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;*/

public class MainActivity extends Activity {

    public WebView webView;
    private String uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=(WebView)findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient());
        //webView.getSettings().setJavaScriptEnabled(true);
        Log.d("msg_info", "haber yΓΌkleniyor");
        webView.loadUrl(uri);
 }
}
+4
source share

Source: https://habr.com/ru/post/1599526/


All Articles