2017-06-20 11 views
5

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

private void showNotification() { 
    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
      | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, 0); 

    Bitmap icon = BitmapFactory.decodeResource(getResources(), 
      R.mipmap.ic_launcher); 

    Notification notification = new NotificationCompat.Builder(getApplicationContext()) 
      .setContentTitle("Revel Is Running") 
      .setTicker("Revel Is Running") 
      .setContentText("Click to stop") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      //.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) 
      .setContentIntent(pendingIntent) 
      .setOngoing(true).build(); 
    startForeground(Constants.FOREGROUND_SERVICE, 
      notification); 
    Log.e(TAG,"notification shown"); 

} 

यहाँ केवल त्रुटि मैं संबंध में देख रहा है: 06-20 12:26:43.635 895-930/? E/NotificationService: Suppressing notification from the package by user request.

उत्तर

10

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

mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager); 
    mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor 
      (ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE); 
    mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build()); 

@TargetApi(26) 
private void createChannel(NotificationManager notificationManager) { 
    String name = "FileDownload"; 
    String description = "Notifications for download status"; 
    int importance = NotificationManager.IMPORTANCE_DEFAULT; 

    NotificationChannel mChannel = new NotificationChannel(name, name, importance); 
    mChannel.setDescription(description); 
    mChannel.enableLights(true); 
    mChannel.setLightColor(Color.BLUE); 
    notificationManager.createNotificationChannel(mChannel); 
} 
+0

क्या इसे संभालने का एक और सुविधाजनक तरीका है? यानी समर्थन/रचना शैली? वही कोड जो "if" और डुप्लिकेट कोड का उपयोग किए बिना प्री -26 और 26+ प्लेटफ़ॉर्म दोनों पर काम करता है। –

+0

यदि (Build.VERSION.SDK_INT> = Build.VERSION_CODES.O) {createNotificationChannel (संदर्भ);} // यहां कोड का REST –

+0

लेकिन कोड के इस नए स्निपेट में, मुझे लगता है कि आप 'mnotifyManager.notify () 'startForeground()' के बजाय '। इस मामले में, क्या यह वास्तव में सेवा अग्रभूमि बना रहा है? –

0

मेरे मामले में सफल कोड है, यह मुझे IntentService का उपयोग कर कारण होता था।

संक्षेप में, यदि आप एक अग्रभूमि सेवा चाहते हैं तो Service उपclass।

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