How to pass a URL link in notification, and on click it should get open in webview?

对着背影说爱祢 提交于 2019-12-13 04:07:12

问题


I am working on a notification project, my requirement is to pass a url link on notification and when user click on notification so it navigates to some activity X and opens that url in the web view which is there in activity X.

This is what i am doing ...

Context context = this.getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//  Intent mIntent = new Intent(this, MainActivity.class);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
Bundle bundle = new Bundle();
bundle.putString("test", "test");
notificationIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

its opening the url but not in my webview rather it is asking to choose browser present in the phone..but i want to open that link in my webview only !!!

Thanks


回答1:


NOTIFICATION SERVICE

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String title = remoteMessage.getNotification().getTitle();
    String body = remoteMessage.getNotification().getBody();
    String intentUri = remoteMessage.getData().get("link");

    sendNotification(intentUri , title , body);
}


//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String url, String title, String body) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    if (url != null) {
        intent.putExtra("URL", url);
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    String channelId = "Default";
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(body);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSound(defaultSoundUri);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

WEBVIEWACTIVITY

  Intent intent = this.getIntent();

    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("URL"))
        {
            Url = extras.getString("URL");
        }
    }
    if (Url != null) {
        webView.loadUrl(Url);
    } else {
        webView.loadUrl("https://www.example.com");
    }



回答2:


You'll have to create a new activity with webview.

use this code in your webview activity

public class WebViewActivity extends Activity {

    private WebView webView;
Bundle extras;
String loadurl;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
extras=getIntent().getExtras();
if(extras!=null){
loadurl=extras.getString("url","your default web page");
}

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(loadurl);
    }
}



回答3:


No need to pass the url in Notification. Pass it as a String in the notification and at receiver end convert that string into url.

For example:

Bundle bundle = new Bundle();
bundle.putString("url", "www.google.com");
notificationIntent.putExtras(bundle);

At receiverEnd convert it like this,

String url = getArguments.getString("url");
Url myUrl = new Url(url);

open myUrl in webView.



来源:https://stackoverflow.com/questions/36623497/how-to-pass-a-url-link-in-notification-and-on-click-it-should-get-open-in-webvi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!