2012-04-25 21 views
5

पर क्लिक करने के बाद गतिविधि खोलना मैं एंड्रॉइड प्रोग्रामिंग के लिए एक बड़ा नोब हूं इसलिए क्षमा करें यदि यह एक आसान काम है। मैंने पुश अधिसूचनाओं के लिए वोगेला पुश अधिसूचना ट्यूटोरियल का बहुत पालन किया (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html)। मैंने कुछ अन्य स्टैक ओवरफ़्लो प्रश्न पढ़े हैं लेकिन मुझे अधिसूचना मिलने के बाद एक इरादा खोलने के बारे में थोड़ा उलझन में है।पुश अधिसूचना एंड्रॉइड

उदाहरण के लिए, अगर मैं सिर्फ एक वेबसाइट पर जाने के लिए अधिसूचना चाहता था, तो यह कैसे काम करेगा? क्या इसे मेरे संदेश रिसीव एक्टिविटी या किसी अन्य प्रोजेक्ट/क्लास के साथ मिलकर जाना होगा?

धन्यवाद

यहाँ कोड मैं अपने 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); 

} 

}

उत्तर

9

अगर आप इस कोशिश अधिसूचना क्लिक पर एक वेबसाइट खोलना चाहते हैं:

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

जब मैं ऐसा करता हूं, तो मेरी पुश अधिसूचना पर क्लिक करने के बाद, मुझे केवल "नया संदेश प्राप्त हुआ" संदेश दिखाई देता है। हो सकता है कि इरादे को खोलने के लिए लंबित इंंटेंट को कुछ बताने की ज़रूरत है? – Kevin

+0

कभी नहीं, इसे अपने कोड के माध्यम से समझ लिया। केवल 1 छोटी चीज को बदलना पड़ा। धन्यवाद! – Kevin

0

C2DM या वर्ग कि extentd आधार रिसीवर के लिए अपने आधार रिसीवर में के लिए है, तो आप एक handleMessage है() है ::

नीचे हैंडल संदेश के लिए नमूना कोड है जो गतिविधि को लॉन्च करता है ::

@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

क्षमा करें, लेकिन आधार रिसीवर मतलब है संदेश रिसीवर या पंजीकरण रिसीवर? मैंने माना कि गतिविधि MessageReceivedActivity के तहत जाएगी जो गतिविधि को बढ़ाती है। लेकिन क्या यह MessageReceiver के तहत जाएगा जो ब्रॉडकास्ट रिसीवर बढ़ाता है? – Kevin

+0

क्या आप अपना कुछ कोड जोड़ सकते हैं? –

+0

मैंने अपना कोड शामिल करने के लिए अपना प्रश्न संपादित किया। तो उदाहरण के लिए, मान लीजिए कि मैं अधिसूचना पर क्लिक करके www.google.com खोलना चाहता हूं? मैं समझता हूं कि इरादे से इसे कैसे किया जाए, यह सुनिश्चित न करें कि कितना लंबित हैन्टेंट – Kevin

संबंधित मुद्दे