Android点击通知打开网页

时间:2015-12-16 15:51:46

标签: java android

我在点击通知时尝试打开网页:

public void shownNotification(String title, String message){
    Intent notifyIntent = new Intent(this, MainActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notifyIntent.setData(Uri.parse("htp://www.google.com"));
    PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingItent)
            .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

我收到通知但没有问题,但是当我点击它时,它只是将我带到应用程序。

1 个答案:

答案 0 :(得分:1)

将您的Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com")); 更改为:

public void shownNotification(String title, String message){
    Intent notifyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingItent)
            .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

然后您的代码将是:

avg(-,-)