android - 如何在 Android 中点击通知时打开浏览器?

标签 android

这是我的代码,我想在用户点击通知时打开一个 URL。

这是代码:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));

Notification myNotification = new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setAutoCancel(false)
                    .setLargeIcon(remote_picture)
                    .setContentTitle(onvan)
                    .setContentIntent(notificationIntent)
                    .setContentText(msg)
                    .setStyle(notiStyle)
                    .build();
notificationManager.notify(1, myNotification);

我收到这个错误: NotificationCompat.Builder 类型中的方法 setContentIntent(PendingIntent) 不适用于参数 (Intent)

我做错了什么?当用户点击通知时,我如何告诉它打开浏览器?

最佳答案

您必须向通知添加待定 Intent 。

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));

PendingIntent pending = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.setContentIntent(pending);

关于android - 如何在 Android 中点击通知时打开浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31889415/

相关文章:

Android studio 3.1 运行命令不重建APK

android - 从 GeoPoints 创建 GPX 文件

android - picasso 不从本地文件加载图像

带有消息的 Android 自定义 ProgressDialog

java - Android Studio : android.intent.category.LAUNCHER 客户端尚未准备好

android - 程序类型已经存在 : com. google.android.gms.common.util.VisibleForTesting

android - 将 Android 11 中的文件保存到外部存储(SDK 30)

android - 在字符串中使用 HTML

android - 'gradlew' 不是内部或外部命令、可运行程序或批处理文件

c# - MvvmCross android 链接器终止绑定(bind)