如何在通知中传递URL链接,并且单击该链接应在webview中打开? [英] How to pass a URL link in notification, and on click it should get open in webview?

查看:173
本文介绍了如何在通知中传递URL链接,并且单击该链接应在webview中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个通知项目,我的要求是在通知上传递一个URL链接,当用户单击通知时,它导航到某个活动X,并在活动X中的Web视图中打开该URL./p>

这就是我在做什么...

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

它打开URL,但不在我的webview中,而是要求选择手机中存在的浏览器..但是我只想在我的webview中打开该链接!!!

谢谢

解决方案

通知服务

@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");
    }

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

解决方案

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

这篇关于如何在通知中传递URL链接,并且单击该链接应在webview中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆