Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Browser on push notification

I am trying to open the browser with a url when the user click on the push notification, i search in stackoverflow and i find this

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);

but it doesnt work for me, when i put that the notification doesnt appear, i debugged it and it only throw the class file editor no error or anything.

this is the code

   public void mostrarNotificacion(Context context, String body,String title, String icon, String url,String superior)
{

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notManager = (NotificationManager) context.getSystemService(ns);




    int icono = R.drawable.mydrawable;
    CharSequence textoEstado = superior;
    long hora = System.currentTimeMillis();

    Notification notif = new Notification(icono, textoEstado, hora);


    Context contexto = context.getApplicationContext();
    CharSequence titulo = title;
    CharSequence descripcion = body;
    PendingIntent contIntent;
    if(url.equalsIgnoreCase("NULL"))
    {
        Intent notIntent = new Intent(contexto,MainActivity.class);
        contIntent = PendingIntent.getActivity(
                contexto, 0, notIntent, 0);
    }
    else
    {            
        // Intent i = new Intent(Intent.ACTION_VIEW);
         //i.setData(Uri.parse(url));
        // contIntent = PendingIntent.getActivity(contexto, 0, i, 0);   
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);



    }


   // notif.setLatestEventInfo(contexto, titulo, descripcion, contIntent);



    //AutoCancel: 
    notif.flags |= Notification.FLAG_AUTO_CANCEL;


    //send notif
    notManager.notify(1, notif);
}
like image 707
D4rWiNS Avatar asked May 13 '13 16:05

D4rWiNS


1 Answers

What you need to do is set a pending intent - which will be invoked when the user clicks the notification. (Above you just started an activity...)

Here's a sample code :

private void createNotification(String text, String link){

    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
    .setAutoCancel(true)
    .setSmallIcon(R.drawable.app_icon)
    .setContentTitle(text);

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

    // pending implicit intent to view url
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.setData(Uri.parse(link));

    PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pending);

    // using the same tag and Id causes the new notification to replace an existing one
    mNotificationManager.notify(String.valueOf(System.currentTimeMillis()), PUSH, notificationBuilder.build());
}

Edit 1 : I changed the answer to use PendingIntent.FLAG_UPDATE_CURRENT for the sample purpose. Thanks Aviv Ben Shabat for the comment.

Edit 2 : Following Alex Zezekalo's comment, note that opening the notification from the lock screen, assuming chrome is used, will fail as explained in the open issue : https://code.google.com/p/chromium/issues/detail?id=455126 -
Chrome will ignore the intent, and you should be able to find in your logcat - E/cr_document.CLActivity﹕ Ignoring intent: Intent { act=android.intent.action.VIEW dat=http://google.com/... flg=0x1000c000 cmp=com.android.chrome/com.google.android.apps.chrome.Main (has extras) }

like image 95
Dror Fichman Avatar answered Oct 12 '22 17:10

Dror Fichman