2012-04-25 32 views
5

Tôi là một noob rất lớn để lập trình Android nên xin lỗi nếu đây là một nhiệm vụ đơn giản. Tôi khá nhiều theo hướng dẫn thông báo đẩy Vogella cho các thông báo đẩy (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html). Tôi đã đọc một số câu hỏi tràn ngăn xếp khác nhưng tôi hơi bối rối về cách mở mục đích khi tôi nhận được thông báo.Mở hoạt động sau khi nhấp vào thông báo đẩy android

Ví dụ: nếu tôi chỉ muốn thông báo đưa tôi đến một trang web, hoạt động đó sẽ hoạt động như thế nào? Nó có phải đi theo MessageReceivedActivity của tôi hoặc một dự án/lớp khác với nhau?

Cảm ơn

Đây là mã tôi đã cho C2DMMessageReceiver tôi

@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); 

} 

}

Trả lời

9

nếu bạn muốn mở một trang web trên thông báo nhấp chuột thử điều này:

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

Khi tôi làm điều này, sau khi nhấp vào thông báo đẩy của tôi, tôi chỉ thấy thông báo "Tin nhắn mới nhận được". Có lẽ một cái gì đó cần phải nói với pendingIntent để mở ý định? – Kevin

+0

Nevermind, tìm ra nó thông qua mã của bạn. Chỉ cần thay đổi 1 điều nhỏ. Cảm ơn! – Kevin

0

Trong nhận cơ sở của bạn cho C2DM hoặc lớp mà cơ sở extentd reciever bạn có một handleMessage() ::

Dưới đây là mã mẫu cho thông báo xử lý khởi chạy hoạt động ::

@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

Xin lỗi nhưng không nhận cơ sở có nghĩa là tin nhắn nhận hoặc đăng ký nhận? Tôi cho rằng hoạt động sẽ đi theo MessageReceivedActivity mở rộng Hoạt động. Nhưng nó sẽ đi theo MessageReceiver mà mở rộng Broadcast Receiver? – Kevin

+0

bạn có thể thêm một số mã của mình không? –

+0

Tôi đã chỉnh sửa câu hỏi của mình để đưa mã của mình vào. Vì vậy, ví dụ như sake, giả sử tôi muốn www.google.com được mở khi nhấp vào thông báo? Tôi hiểu làm thế nào để làm điều đó với Intent, không chắc chắn như thế nào pendingIntent hoạt động – Kevin

Các vấn đề liên quan