13

नहीं दिखाती है, अब तक, मैंने अपनी सेवा शुरू करने के लिए ContextCompat.startForegroundService(context, intentService); का उपयोग करने के लिए अपना कोड निर्धारित कर दिया है। इस तरह, यह एंड्रॉइड < 26 और एंड्रॉइड 26 (ओरेओ) पर भी काम करता है।ओरेओ - अग्रभूमि सेवा अग्रभूमि अधिसूचना

मुझे अभी भी एक अंतर दिखाई देता है, एंड्रॉइड अयस्क में मुझे अपनी कस्टम अग्रभूमि अधिसूचना नहीं दिखाई देती है (मुझे केवल "ऐप पृष्ठभूमि में चल रहा है" अधिसूचना दिखाई देती है)। क्या मुझे वहां कुछ भी समायोजित करना है?

मेरी सेवा इस तरह दिखता है:

public class BaseOverlayService extends Service { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     moveToForeground(); 
    } 

    private void moveToForeground() { 
     Notification notification = ...; 
     super.startForeground(NOTIFICATION_ID, notification); 
    } 
} 

सरकारी उदाहरण

इस उदाहरण (https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200) टिप्पणी के रूप में पता चलता है, कि मैं निम्नलिखित का उपयोग करना चाहिए, लेकिन startServiceInForeground मौजूद नहीं है ...

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { 
    NotificationManager mNotificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); 
    mNotificationManager.startServiceInForeground(new Intent(this, BaseOverlayService.class), NOTIFICATION_ID, notification); 
} else { 
    startForeground(NOTIFICATION_ID, notification); 
} 

संपादित करें

मेरी सूचना अब तक इस, जो एपीआई < 26 के साथ android उपकरणों के हजारों पर काम कर रहा है की तरह बनाई गई है:

protected Notification foregroundNotification(int notificationId) 
{ 
    boolean paused = MainApp.getPrefs().sidebarServicePaused(); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setSmallIcon(R.drawable.icon_not); 
    builder.setContentIntent(notificationIntent()); 
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon)); 
    builder.setContentTitle(getString(R.string.derived_app_name)); 
    builder.setContentText(getString(checkStatus() ? (paused ? R.string.service_notification_text_paused : R.string.service_notification_text_running) : R.string.service_notification_text_preparing)); 
    builder.setColor(Color.parseColor("#25baa2")); 

    if (MainApp.getPrefs().hideNotificationIcon()) 
     builder.setPriority(NotificationCompat.PRIORITY_MIN); 
    else 
     builder.setPriority(NotificationCompat.PRIORITY_MAX); 

    if (paused) 
     builder.addAction(R.drawable.ic_play_arrow_black_24dp, getString(R.string.resume), resumeIntent(this)); 
    else 
     builder.addAction(R.drawable.ic_pause_black_24dp, getString(R.string.pause), pauseIntent(this)); 

    return builder.build(); 
} 

उत्तर

11

आप अपने अनुप्रयोग के लिए अधिसूचना चैनल परिभाषित करने की आवश्यकता हो सकती है। लॉग की जांच करें, चेतावनी होनी चाहिए। परिचय this परिचय

मैं आपको कुछ उदाहरण दूंगा, यह कोटिन में होगा। सबसे पहले, इस तरह की कक्षा बनाएं। आपको अपने आवेदन की शुरुआत में एक बार createMainNotificationChannel() को कॉल करने की आवश्यकता होगी।

class NotificationManager(private val context: Context) { 

    companion object { 
     private val CHANNEL_ID = "YOUR_CHANNEL_ID" 
     private val CHANNEL_NAME = "Your human readable notification channel name" 
     private val CHANNEL_DESCRIPTION = "description" 
    } 

    @RequiresApi(Build.VERSION_CODES.O) 
    fun getMainNotificationId(): String { 
     return CHANNEL_ID 
    } 

    @RequiresApi(Build.VERSION_CODES.O) 
    fun createMainNotificationChannel() { 
      val id = CHANNEL_ID 
      val name = CHANNEL_NAME 
      val description = CHANNEL_DESCRIPTION 
      val importance = android.app.NotificationManager.IMPORTANCE_LOW 
      val mChannel = NotificationChannel(id, name, importance) 
      mChannel.description = description 
      mChannel.enableLights(true) 
      mChannel.lightColor = Color.RED 
      mChannel.enableVibration(true) 
      val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager 
      mNotificationManager.createNotificationChannel(mChannel) 
    } 
} 

तो फिर तुम धन्यवाद का उपयोग कर सकते util इस

fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId) 
    } else { 
     return NotificationCompat.Builder(context) 
    } 
} 
+0

तरह लिंक आपके द्वारा जोड़े गए वैसे भी काफी अच्छा है। हालांकि एक सवाल: क्या इस परिवर्तन का मतलब है कि मुझे एंड्रॉइड अयस्क में अब अग्रभूमि सेवा के लिए अधिसूचना दिखाने की ज़रूरत नहीं है? क्योंकि लोगों ने मुझे बताया था कि मेरा ऐप अधिसूचना के बिना एंड्रॉइड अयस्क पर भी काम कर रहा है ... – prom85

+1

eghmmm ... वास्तव में यह अधिसूचना के बिना काम नहीं कर सकता है। गतिविधि या अधिसूचना जैसे दृश्य घटकों के बिना एंड्रॉइड ओ में पृष्ठभूमि कार्य की अनुमति नहीं है। यदि आप बिना किसी घटक घटक प्रणाली के अपनी सेवा चलाते हैं तो कुछ समय बाद इसे मार देंगे। इसलिए यदि आप पृष्ठभूमि नौकरी करना चाहते हैं तो आपको सेवा के लिए अधिसूचना की आवश्यकता है। यदि आपको कुछ अवधि के साथ पृष्ठभूमि में कुछ चलाने की आवश्यकता है - आपको [फायरबेस जॉब प्रेषक] (https://github.com/firebase/firebase-jobdispatcher-android) की जांच करनी चाहिए। यह आपको अनुसूचित सेवा चलाने की अनुमति देता है। –

+0

मेरे पास 'WindowManager' पर यूआई तत्वों के साथ एक साइडबार ऐप है और यह अब तक उपयोग की जाने वाली अदृश्य अधिसूचना के साथ काम करता प्रतीत होता है (अदृश्य अधिसूचना एक चैनल के बिना एक है)। कुछ दिनों में अब कोई शिकायत नहीं मिली है। एम्यूलेटर 5 सेकंड के भीतर मेरी सेवा को मार नहीं देता है या तो – prom85

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