0

quiero abrir desde una notificacion el acitivity main y otro dependiendo de un parametro pero solo puedo abrir el pricipal y al recibir notificaciones con la app abierta esta se cierra el codigo es el siguiente.

private void enviarNotificacion(RemoteMessage.Notification messageBody) {                                                           
    Intent intent;                                                                                                                  
    Notification notificacion;                                                                                                      

    if(messageBody.getClickAction().compareTo(null)==0){                                                                            
        intent = new Intent(getApplicationContext(),MainActivity.class);                                                            
    }else{                                                                                                                          
        intent = new Intent(getApplicationContext(),MostrarWeb.class);                                                               
    }                                                                                                                               


    PendingIntent pendingIntent = 
    PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);                                 

    Uri defaultSoundUri= 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);                                          
    notificacion = new NotificationCompat.Builder(this)                                                                             
        .setSmallIcon(getResources().getIdentifier(messageBody.getIcon(), 
        "drawable", getApplicationContext().getPackageName()))
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
         R.drawable.ic_noti))                                         
        .setContentTitle(messageBody.getTitle())                                                                                
        .setContentText(messageBody.getBody())                                                                                  
        .setAutoCancel(true)                                                                                                    
        .setContentIntent(pendingIntent)                                                                                        
        .setSound(defaultSoundUri)                                                                                              
        .setContentIntent(pendingIntent)                                                                                        
        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})                                                                   
        .build();                                                                                                               


    NotificationManager notificationManager =                                                                                       
        (NotificationManager) 
    getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);                           

    notificationManager.notify(0,notificacion);}
3
  • Para dar un a respuesta más precisa te sugiero agregues que datos se obtienen en messageBody.
    – Jorgesys
    el 17 abr. 2018 a las 21:02
  • No abre MostrarWeb.class? que muestra el Logcat?
    – armen
    el 18 abr. 2018 a las 1:27
  • El logcat no pone nada simplemente desaparece la notificación y no abre nada, aunque el main si lo abre sabrías decirme como evitar que la App se cierre cuando recibo una notificación?
    – aitorlv
    el 25 abr. 2018 a las 21:57

2 respuestas 2

1

Tendrías que crear un Intent tal que así:

Intent nombreIntent = new Intent(CurrentActivity.this, MostrarWeb.class);
CurrentActivity.this.startActivity(nombreIntent);

Y en el manifest xml ( app > manifests > AndroidManifest.xml ):

<activity android:label="@string/app_name" android:name="MostrarWeb"/>
1

Usando messageBody.getClickAction() en onMessageReceived() siempre retornará un valor null, obtén información de esta forma especificando el nombre del key :

messageBody.getData().get(<nombre de key>) 

pero para esto debes asegurarte que el payload en realidad tenga este key y su valor:

 'click_action' => 'provengo de la activity ...'
2
  • 1
    te recomiendo mirar esta pregunta , tiene una respuesta muy buena por parte de @Elenasys es.stackoverflow.com/questions/151752/… el 18 abr. 2018 a las 18:05
  • lo he cambiado como en el ejemplo es mas estoy usando el mismo codigo php mi pregunta es ¿para abrir n ativity desde la notificacion necesita estar definido como launcher?
    – aitorlv
    el 23 abr. 2018 a las 21:26

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.