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

moh*_*eri 2 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);
Run Code Online (Sandbox Code Playgroud)

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

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

Dee*_*yal 7

您必须将挂起的意图添加到通知中。

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);
Run Code Online (Sandbox Code Playgroud)