2012-04-25 25 views
5

Android programlama için büyük bir noob çok üzgünüm, bu basit bir görevse. Hemen hemen push bildirimleri için Vogella push notification eğitimini takip ettim (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html). Başka bir yığın taşma sorusu okudum ama bildirim aldığımda bir niyetin nasıl açılacağı konusunda biraz kafam karışmış durumda.İtme bildirimi tıklatıldıktan sonra açılış etkinliği android

Örneğin, yalnızca bildirimin beni bir web sitesine yönlendirmesini istemiş olsaydım, bu nasıl çalışır? MessageReceivedActivity veya başka bir proje/sınıfım altında hep birlikte mi olmalı?

Teşekkür İşte

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

} 

} C2DM veya sınıf o extentd taban alıcısı ilişkin taban alıcısı olarak

cevap

9

Bunu deneyin bildirim tıklandığında bir web sitesi açmak istiyorsanız:

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

Bunu yaptığımda, push bildirimi tıkladıktan sonra yalnızca "Yeni ileti alındı" iletisini görüyorum. Belki de bir şeyin niyetini açmak için bekleyen birisine söylemesi gerekiyor? – Kevin

+0

Nevermind, kodunuzdan anladım. Sadece 1 küçük şey değiştirmek zorunda kaldı. Teşekkürler! – Kevin

0

için sahip kod (a handleMessage var) 'dir ::

Aktiviteyi başlatan tanıtıcı iletinin örnek kodu aşağıdadır ::

@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

Üzgünüz ancak baz alıcı demek İleti Alıcı veya Kayıt Alıcı? Etkinliğin Aktiviteyi genişleten MessageReceivedActivity kapsamında olacağını varsaydım. Ama Broadcast Receiver'ı uzatan MessageReceiver’in altında mı? – Kevin

+0

Kodunuzun bir kısmını ekleyebilir misiniz? –

+0

Sorumu kodumu eklemek için düzenledim. Öyleyse, sake, bildirimin üzerine tıklayarak www.google.com'un açılmasını istediğimi varsayalım. Niyetle nasıl yapılacağını anlıyorum, pendingIntent'in nasıl çalıştığından emin değil – Kevin

İlgili konular