2013-05-13 28 views
9

Sto cercando di aprire il browser con un URL quando l'utente clicca sulla notifica push, che cerco in StackOverflow e trovo questoBrowser Inaugurazione notifica push

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

ma doesnt lavoro per me, quando ho messo che la notifica non appare, ho il debug e solo lanciare l'editor di file di classe nessun errore o altro.

Questo è il codice

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); 
} 
+0

Dove chiami il 'mostrarNotificacion' me THOD? – Eran

+0

Ora funziona grazie a dror fichman, comunque chiamo il metodo onMessage – D4rWiNS

+0

Ecco una soluzione alternativa http://stackoverflow.com/questions/39285006/android-lock-screen-notification-is-notable-to-open- il collegamento –

risposta

22

Quello che dovete fare è impostare un intento in attesa - che verrà richiamato quando l'utente fa clic la notifica. (Sopra hai appena iniziato un'attività ...)

Ecco un esempio di codice:

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: Ho cambiato la risposta da utilizzare per lo scopo PendingIntent.FLAG_UPDATE_CURRENT campione. Grazie Aviv Ben Shabat per il commento.

Edit 2: seguito il commento di Alex Zezekalo, notare che aprendo la notifica dalla schermata di blocco, assumendo cromo viene utilizzato, non riuscirà come spiegato nella questione aperta: https://code.google.com/p/chromium/issues/detail?id=455126-
Chrome ignorerà l'intento , e dovresti riuscire a trovare nel tuo logcat - E/cr_document.CLActivity: Intento ignorato: Intent {act = android.intent.action.VIEW dat = http://google.com/ ... flg = 0x1000c000 cmp = com.android.chrome/com.google.android.apps.chrome.Main (ha comparse)}

+2

del browser con doppio tocco deve avere http: // ' – Hamidreza

+0

Intent.FLAG_ACTIVITY_NEW_TASK non deve essere utilizzato qui. Come dice la documentazione: Deve essere uno o più di: PendingIntent.FLAG_ONE_SHOT, PendingIntent.FLAG_NO_CREATE, PendingIntent.FLAG_UPDATE_CURRENT, Intent.FILL_IN_ACTION, Intent.FILL_IN_DATA, Intent.FILL_IN_CATEGORIES ...) –

+0

Hmm, è una buona soluzione ma non funziona quando tocco la notifica push su Blocco schermo: in tal caso il browser non funziona e non apre il collegamento. Qualche idea? –