Apertura del navegador en la notificación push
Estoy tratando de abrir el navegador con una url cuando el usuario haga clic en la notificación push, busco en stackoverflow y encuentro esto
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent);
Pero no funciona para mí, cuando pongo que la notificación no aparece, lo depuré y sólo lanzar el editor de archivos de clase ningún error o nada.
- Cómo dar contador si hay más de una notificación
- Registro de GCM con dos identificadores de registro de trabajo diferentes
- Registro de identificación del dispositivo directamente con Amazon SNS
- Cordova PushPlugin - No se puede recibir notificación de GCM en android si la aplicación no se está ejecutando
- Android: Notificaciones push de GCM que no aparecen en la lista de notificaciones
Este es el codigo
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); }
- Notificación de cancelación automática no funciona para Android Lollipop
- Visualización de emojis en el contenido de la notificación
- Haga clic en notificación para ingresar mi aplicación en android
- ¿Debería llamar a WakeLock antes de crear una notificación?
- Java.lang.SecurityException: Requiere permiso de VIBRATE en Jelly Bean 4.2
- Notificaciones push en la plataforma Android
- Error de Google Android GCM NotRegistered
- Programación de Android: GCMIntentService Stuck at WakeLock
Lo que debe hacer es establecer una intención pendiente, que se invocará cuando el usuario haga clic en la notificación. (Sobre usted apenas comenzó una actividad …)
He aquí un ejemplo de código:
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()); }
Editar 1: He cambiado la respuesta para utilizar PendingIntent.FLAG_UPDATE_CURRENT
para el propósito de la muestra. Gracias Aviv Ben Shabat por el comentario.
Editar 2: Después del comentario de Alex Zezekalo, tenga en cuenta que al abrir la notificación desde la pantalla de bloqueo, suponiendo que se utilice cromo, se producirá un error como se explica en el problema abierto: https://code.google.com/p/chromium/issues/detail? Id = 455126 –
Chrome ignorará la intención, y deberías poder encontrarla en tu logcat – E / cr_document.CLActivity: Ignorando la intención: Intent {act = android.intent.action.VIEW dat = http://google.com/ … Flg = 0x1000c000 cmp = com.android.chrome / com.google.android.apps.chrome.Main (tiene extras)}