0

I'm developing an android app. It receive with a push notification an URL. I need to open the url with chrome. I'm trying with this but is not working

Intent i;
    try {
        i = new Intent(Intent.ACTION_VIEW);
        i.setType("text/html");
        i.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
        i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
        i.addCategory("android.intent.category.LAUNCHER");
        i.setData(Uri.parse(audioLink));
    } catch (ActivityNotFoundException ex) {
        i = new Intent(Intent.ACTION_VIEW, Uri.parse(audioLink));
    }

    PendingIntent pendingIntent =
            PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder;
    NotificationManager manager;

    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    builder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.newmessagetitle))
            .setContentText(getString(R.string.newmessage))
            .setSmallIcon(R.mipmap.ic_stat_lollipop_icon);

    builder.setContentIntent(pendingIntent);

    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    builder.setDefaults(defaults);
    builder.setAutoCancel(true);

    manager.notify(notifyID, builder.build());
Manuel Castro
  • 1,633
  • 3
  • 24
  • 38
  • Can you use ACTION_VIEW instead of ACTION_MAIN in the first case, and specify the MIME_TYPE? – geokavel Oct 05 '15 at 16:30
  • What do you mean with specify the MIME_TYPE? – Manuel Castro Oct 05 '15 at 16:37
  • i.setType(type), and use a MIME_TYPE representing the type of data it is, such as the ones here: https://developer.android.com/intl/zh-tw/guide/components/intents-common.html#Browser – geokavel Oct 05 '15 at 16:41
  • Here are MIME_TYPEs for all the different file extensions. Use the one that fits the type of file you're trying to open. http://www.sitepoint.com/web-foundations/mime-types-complete-list/ – geokavel Oct 05 '15 at 16:44
  • I tried with this but still not working. I edited the code so you can check if I understood what you mean – Manuel Castro Oct 05 '15 at 16:46
  • When it's not working what exception do you get? – geokavel Oct 05 '15 at 16:47
  • since you have com.android.chrome.Main later on I would change it back to main. Also, I don't know if this is causing the problem, but I think you should take out the i.addCategroy() part – geokavel Oct 05 '15 at 16:52
  • It does not send me an error. Basically when I click on the notification it does nothing – Manuel Castro Oct 06 '15 at 08:43

1 Answers1

1

I really think something like this should work for the intent code. If not, there is probably something wrong with another part of your code:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(audioLink));
i.setPackage("com.android.chrome");

By the way the ActivityNotFoundException is totally useless in that place, because only Activity.startActivity() throws that exception. If you want to check if Chrome is installed, I would use this: How can I learn whether a particular package exists on my Android device?

Community
  • 1
  • 1
geokavel
  • 619
  • 4
  • 12