6

मैं अपने एंड्रॉइड एप्लिकेशन को बनाने के लिए कॉर्डोवा का उपयोग कर रहा हूं। चूंकि एंड्रॉइड सेवा मारता है, इसलिए मैं सेवा मारने से बचने के लिए अधिसूचना के साथ बाध्यकारी सेवा कर रहा हूं।कॉर्डोवा - पृष्ठभूमि चल रही सेवा को सूचित करें

यहाँ मेरी विधि मैं कैसे अधिसूचना

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    context = this.getApplicationContext(); 
    notifyService(); 

    return START_NOT_STICKY; 
} 

private void notifyService() { 
    String package_name = this.getApplication().getPackageName(); 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

    Bitmap icon = BitmapFactory.decodeResource(getResources(), 
      this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name)); 

    Notification notification = new NotificationCompat.Builder(this) 
      .setContentTitle("Smart Home") 
      .setContentText("Smart Home running in background") 
.setSmallIcon(this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name)) 
      .setContentIntent(pendingIntent) 
      .setOngoing(true) 
      .build(); 

    startForeground(notificationId, notification); 
} 

साथ सेवा बाँध यहाँ उत्पादन

enter image description here

अधिसूचना उत्पन्न होता है, लेकिन जैसा कि मैंने सेट अधिसूचना शीर्षक नहीं है। साथ ही, जब मैं इस अधिसूचना पर क्लिक करता हूं, तो यह ऐप जानकारी गतिविधि में जा रहा है। लेकिन मैं अपनी मुख्य गतिविधि में जाना चाहता हूं।

क्या किसी को भी इस समस्या का सामना करना पड़ा है? या मेरे कोड को कॉर्डोवा के लिए कोई बदलाव चाहिए?

+0

क्या आप पृष्ठभूमि में सेवा के रूप में कॉर्डोवा अधिसूचना ऐप चाहते हैं? क्या यह आपका सवाल है? – Gandhi

+0

@ गंधी मैं अपनी प्लगइन से एक सेवा शुरू कर रहा हूं। और मैं नहीं चाहता कि एंड्रॉइड इसे मार डाले। तो मैं एक अग्रभूमि अधिसूचना का उपयोग कर रहा हूँ। मैं अधिसूचना के साथ इसे सूचित कर सकता हूं लेकिन उत्पन्न अधिसूचना मेरी अधिसूचना –

+0

शर्मिंदा नहीं है क्या आपने इस प्लगइन को देखा - https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-Background- सेवा – Gandhi

उत्तर

3

एक लंबे कोशिश के बाद यह पता चल करने के लिए इस परिवर्तन इस लाइन

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

हासिल करना चाहते हैं। समस्या मेरे लंबित इरादे गतिविधि नाम के साथ थी। नीचे कोड मेरे लिए काम किया

String package_name = this.getApplication().getPackageName(); 
Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(package_name); 
+0

[नरेश कुमार] (http://stackoverflow.com/a/42759500/984830) से जवाब स्वीकार करने में क्या गलत था, जिसने आपको यह समाधान दिया? –

+0

@NickCardoso क्षमा करें मेरे लंबित इरादे से क्या गलत है यह निर्दिष्ट करने के लिए क्षमा करें। अधिसूचनाइन्टेंट मैं लंबित पास किया गयान्टेंट गलत था। यह इरादा अधिसूचनाइन्टेंट = context.getPackageManager() होना चाहिए। GetLaunchIntentForPackage (package_name); –

+0

अच्छी स्पष्टीकरण, आपको शायद अपने उत्तर jsut को उन रेखाओं तक सीमित कर देना चाहिए जो इसे बदलते हैं, इसे स्पष्ट करने के लिए –

3

जब मैं इस अधिसूचना पर क्लिक करता हूं, तो यह ऐप जानकारी गतिविधि में जा रहा है। लेकिन मैं अपने मुख्य गतिविधि को स्थानांतरित करने के लिए इस लाइन

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

आशा है कि यह मदद करता है आप

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