java - Android点击通知打开网页

标签 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);
}

我毫无问题地收到了通知,但是当我单击它时,它只是将我带到应用程序。

最佳答案

将您的 notifyIntent 更改为:

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

关于java - Android点击通知打开网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316397/

相关文章:

java - 在 Valgrind 下的 Sun VM 中运行 JNI 应用程序

java - assert(false) 不会停止执行

java - Array List 在 addListenerForSingleValueEvent 监听器方法后变为空

android - Google Map API 无法插入到包含 Google Map Json 的 URL 中

FFmpeg - 无法识别的选项 'map [outv]'

java - 如何将 NewProxyConnection 类型转换为 AS400JDBCConnection

java - 在Java中查找方法的调用者(反射)

php - 流行的 Android 应用程序如何连接到服务器?

c# - 使用 Xamarin 下载文件

java - 错误数据库未打开