21

मैंने अपने जीसीएमआईएनटीन्ट्स सेवा में एक कोड लिखा है जो कई उपयोगकर्ताओं को पुश नोटिफिकेशन भेजता है। मैं अधिसूचना प्रबंधक का उपयोग करता हूं जो अधिसूचना क्लिक होने पर वर्णन एक्टिविटी क्लास को कॉल करेगा। मैं भी भेज event_id DescriptionActivityलंबित इरादे का उपयोग कर putExtra

protected void onMessage(Context ctx, Intent intent) { 
    message = intent.getStringExtra("message"); 
    String tempmsg=message; 
    if(message.contains("You")) 
    { 
     String temparray[]=tempmsg.split("="); 
     event_id=temparray[1]; 
    } 
    nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    intent = new Intent(this, DescriptionActivity.class); 
    Log.i("the event id in the service is",event_id+""); 
    intent.putExtra("event_id", event_id); 
    intent.putExtra("gcmevent",true); 
    PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0); 
    String title="Event Notifier"; 
    Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis()); 
    n.setLatestEventInfo(this, title, message, pi); 
    n.defaults= Notification.DEFAULT_ALL; 
    nm.notify(uniqueID,n); 
    sendGCMIntent(ctx, message); 

} 

यहाँ event_id कि मैं उपरोक्त विधि में हो रही करने के लिए GCMIntentService फार्म सही यानी मैं हमेशा अद्यतन एक मिल है। लेकिन नीचे दिए गए कोड में (विवरण एक्टिविटी.जावा):

intent = getIntent(); 
    final Bundle b = intent.getExtras(); 
    event_id = Integer.parseInt(b.getString("event_id")); 

इवेंट_आईडी हमेशा "5" होता है। कोई फर्क नहीं पड़ता कि मैंने GCMIntentService क्लास में एक्स्ट्रा रखा है, event_id मुझे हमेशा मिलता है 5. क्या कोई समस्या को इंगित कर सकता है? लंबित इरादे के कारण है? यदि हां, तो मुझे इसे कैसे संभालना चाहिए?

उत्तर

41

PendingIntent आपके द्वारा प्रदान किए गए पहले Intent के साथ पुन: उपयोग किया जाता है, यह आपकी समस्या है। अतिरिक्त अद्यतन करने के लिए, वैकल्पिक रूप से

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

अगर आप सिर्फ चाहते हैं, झंडा PendingIntent.FLAG_UPDATE_CURRENT

+0

धन्यवाद बहुत आदमी! यह काम किया :) – Nemin

+0

बहुत बढ़िया जवाब! धन्यवाद। –

+0

यदि यह स्पष्ट नहीं था: लंबित INTent pi = लंबित INTent.getActivity (यह, 0, इरादा, लंबित INTent.FLAG_CANCEL_CURRENT); – Alecs

12

का उपयोग करें:

इससे बचने के लिए, झंडा PendingIntent.FLAG_CANCEL_CURRENT का उपयोग करके ऐसी PendingIntent.getActivity() फोन वास्तव में एक नया एक पाने के लिए आपके द्वारा प्रदान किए गए पहले इरादे से लंबित इंटेंट का पुन: उपयोग किया जाता है, जैसा कि यह जोफरी ने कहा था। आप ध्वज लंबित INTent.FLAG_UPDATE_CURRENT का उपयोग करने का प्रयास कर सकते हैं।

PendingIntent pi = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
4

शायद आप अभी भी पुराने इरादे का उपयोग कर रहे हैं। इसे आजमाएं:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    //try using this intent 

    handleIntentExtraFromNotification(intent); 
} 
+0

बहुत बहुत धन्यवाद। मैं इसके साथ जूझ रहा हूं –

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