Открытие ссылки браузера через уведомление не работает

Моя проблема заключается в следующем:

Я отправляю уведомление в панель уведомлений, и я поместил ссылку URI в намерение, отправленное с ним. Как только я нажимаю на уведомление, я получаю диалог, который я хочу сделать, но он показывает мусор, как информация о приложении, сканер штрих-кода, диалог вызова. вместо браузера.

Представляю свой код:

      Intent notificationIntent = new Intent(Intent.ACTION_VIEW);

      PendingIntent contentIntent = PendingIntent.getActivity(contexta, 0, notificationIntent, 0);
      notificationIntent.setData(Uri.parse("http://www.google.com"));
      notification.setLatestEventInfo(contexta, contentTitle, contentText, contentIntent);
      mNotificationManager.notify(970970, notification);

Так что я, вероятно, не думаю в правильном направлении. Должен ли я вставлять намерение и иметь обработчик в моем собственном приложении, вместо этого создать новое намерение для браузера? Но это было бы странно, почему андроид неправильно обрабатывает мои первоначальные намерения.

Как всегда, Любая помощь приветствуется.

Спасибо, Рохан.

Ответ 1

Я думаю, проблема в том, что вы устанавливаете данные в "notificationIntent" после того, как вы передадите их PendingIntent.

Попробуйте следующее:

      Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

      PendingIntent contentIntent = PendingIntent.getActivity(contexta, 0, notificationIntent, 0);
      notification.setLatestEventInfo(contexta, contentTitle, contentText, contentIntent);
      mNotificationManager.notify(970970, notification);

Или попробуйте следующее:

      Intent notificationIntent = new Intent(Intent.ACTION_VIEW);

      notificationIntent.setData(Uri.parse("http://www.google.com"));
      PendingIntent contentIntent = PendingIntent.getActivity(contexta, 0, notificationIntent, 0);
      notification.setLatestEventInfo(contexta, contentTitle, contentText, contentIntent);
      mNotificationManager.notify(970970, notification);

Ответ 2

его работа для меня

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
            notificationIntent.setData(Uri.parse("http://www.google.com")); 
            PendingIntent pi = PendingIntent.getActivity(context, 0, notificationIntent, 0);
             // Resources r = getResources();
              Notification notification = new NotificationCompat.Builder(context)
                      .setTicker("yortext")
                      .setSmallIcon(android.R.drawable.ic_menu_report_image)
                      .setContentTitle("yortext")
                      .setContentText("sdsd")
                      .setContentIntent(pi)
                      .setAutoCancel(true)
                      .build();

              NotificationManager notificationManager2 =  (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
              notificationManager2.notify(0, notification);