28

My problem is the following:

I'm posting a notification to the notifications bar, and i've put a URI link in the intent being sent with it. As soon as i click on the notification i get a dialog what i want to do, but it's showing rubbish like Application info, Barcode scanner, Call dialog. instead of Browser.

I present my code:

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

So i'm probably not thinking in the right direction. Should i perhaps insert an intent and have a handler in my own application create a new intent for the browser instead? But that would be strange, why does android not handle my initial intent correctly then.

As always, Any and all help is greatly appreciated.

Thanks, Rohan.

marienke
  • 2,465
  • 4
  • 34
  • 66
Rohan
  • 8,006
  • 5
  • 24
  • 32

2 Answers2

50

I think the problem is you're setting the data to "notificationIntent" after you give it to PendingIntent.

Try this:

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

Or try this:

      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);
DeeV
  • 516
  • 5
  • 2
10

its work for me

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);
sirmagid
  • 1,090
  • 1
  • 16
  • 23
  • Hi, sirmagid. Your code works great. Do you know what changes have to be made in order to parse the link in the app (webview app in my case) and not in the browser? I'd really appreciate any help. – JohnA10 Nov 11 '16 at 16:37
  • @JohnA10, Do you manage to find the code for parsing. if yes, please post it. – Chris Harris Mar 15 '17 at 15:45
  • @ChrisHarris do you have this question opened in another thread? if I post code here in the answer of an answer, it's going to look bad. – JohnA10 Mar 15 '17 at 19:17
  • @JohnA10, Please post it here - http://stackoverflow.com/questions/40492749/how-to-sent-custom-notification-to-app-users-through-internet – Chris Harris Mar 17 '17 at 15:47