2012-04-25 8 views
5

Soy un novato enorme para la programación de Android, así que lo siento si se trata de una tarea sencilla. Seguí bastante el tutorial de notificación de inserción de Vogella para notificaciones push (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html). He leído algunas otras preguntas de desbordamiento de pila, pero estoy un poco confundido sobre cómo abrir un intento una vez que recibo la notificación.Actividad de apertura después de hacer clic en notificación de inserción android

Por ejemplo, si solo quisiera que la notificación me condujera a un sitio web, ¿cómo funcionaría? ¿Tendría que pasar por debajo de mi MessageReceivedActivity u otro proyecto/clase todos juntos?

Gracias

Aquí está el código que tengo para mi C2DMMessageReceiver

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    Log.w("C2DM", "Message Receiver called"); 
    if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) { 
     Log.w("C2DM", "Received message"); 
     final String payload = intent.getStringExtra("payload"); 
     Log.d("C2DM", "dmControl: payload = " + payload); 
     // TODO Send this to my application server to get the real data 
     // Lets make something visible to show that we received the message 
     createNotification(context, payload); 

    } 
} 

public void createNotification(Context context, String payload) { 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
      "Message received", System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    //adding LED lights to notification 
    notification.defaults |= Notification.DEFAULT_LIGHTS; 

    Intent intent = new Intent(context, MessageReceivedActivity.class); 
    intent.putExtra("payload", payload); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      intent, 0); 
    notification.setLatestEventInfo(context, "Message", 
      "New message received", pendingIntent); 
    notificationManager.notify(0, notification); 

} 

}

Respuesta

9

si desea abrir una página web en la notificación clic intente esto:

public void createNotification(Context context, String payload) { 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, 
       "Message received", System.currentTimeMillis()); 
     // Hide the notification after its selected 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     //adding LED lights to notification 
     notification.defaults |= Notification.DEFAULT_LIGHTS; 

     Intent intent = new Intent("android.intent.action.VIEW", 
     Uri.parse("http://my.example.com/")); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       intent, 0); 
     notification.setLatestEventInfo(context, "Message", 
       "New message received", pendingIntent); 
     notificationManager.notify(0, notification); 

    } 
+0

Cuando hago esto, después de hacer clic en mi notificación de inserción, solo veo el mensaje "Nuevo mensaje recibido". Tal vez algo necesita decirle a la persona pendiente para abrir el intento? – Kevin

+0

No importa, lo descubrió a través de su código. Solo tuve que cambiar 1 pequeña cosa. ¡Gracias! – Kevin

0

En su receptor base para c2dm o la clase de la que reciever base de extentd tiene una handleMessage() ::

A continuación se muestra el código de ejemplo para el mensaje de identificador que inicia la actividad ::

@Override 
    protected void handleMessage(Context context, Intent intent) { 
     String regId = C2DMessaging.getRegistrationId(context); 
     String logKey = this.getClass().getSimpleName(); 
     String title=""; 
     String message=""; 
     if (regId!= null) { 
      if (intent.hasExtra(Constants.TITLE)) { 
       title = intent.getStringExtra(Constants.TITLE); 
      } 
      if(intent.hasExtra(Constants.MESSAGE)){ 
       message = intent.getStringExtra(Constants.MESSAGE); 
      } 
      // TODO Send this to my application server to get the real data 
      // Lets make something visible to show that we received the message 
      if(!title.equals("") && !message.equals("")) 
       createNotificationForMsg(context,title,message); 
     } 
    } 

    @Override 
    public void createNotificationForMsg(Context context,String title,String message) { 
     final String logKey = this.getClass().getSimpleName(); 

     try { 
      NotificationManager notificationManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(R.drawable.icon, 
        "update(s) received", System.currentTimeMillis()); 
      // Hide the notification after its selected 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      //adding sound to notification 
      notification.defaults |= Notification.DEFAULT_SOUND;    

       Intent intent = new Intent(context, YourAlertActivity.class); 

       if(Constants.LOG)Log.d(logKey, Constants.TITLE +": "+ title +" , "+Constants.MESSAGE+": "+message); 
       intent.putExtra(Constants.TITLE, title); 
       intent.putExtra(Constants.MESSAGE, message); 

       PendingIntent pendingIntent = PendingIntent.getActivity(context, Calendar.getInstance().get(Calendar.MILLISECOND), intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
       notification.setLatestEventInfo(context, "Castrol", 
         title+"update Received", pendingIntent); 
       notificationManager.notify(Calendar.getInstance().get(Calendar.MILLISECOND), notification); 



     } catch (Exception e) { 
//   MessageReceivedActivity.view.setText("createNotificationFor Msg: " 
//     + e.toString()); 
     } 
    } 
+1

Receptor Lo sentimos pero ¿significa receptor base del receptor del mensaje o de registro? Supuse que la actividad pasaría a MessageReceivedActivity, que extiende la Actividad. ¿Pero pasaría por MessageReceiver que extiende Broadcast Receiver? – Kevin

+0

puede agregar algo de su código? –

+0

Edité mi pregunta para incluir mi código. Entonces, por ejemplo, sake, digamos que quiero que www.google.com se abra al hacer clic en la notificación. Entiendo cómo hacerlo con Intent, no estoy seguro de cómo pendingIntent funciona – Kevin

Cuestiones relacionadas