13

Şimdiye kadar, hizmetimi başlatmak için kodumu ContextCompat.startForegroundService(context, intentService); kullanmaya yönlendirdim. Bu şekilde, android < 26 ve android 26 (Oreo) üzerinde de çalışır.Oreo - Önplan hizmeti ön plan bildirimini göstermiyor

Hala bir fark görüyorum, android oreo özel ön plan bildirimimi göremiyorum (sadece "app arka planda çalışıyor" bildirimini görüyorum). Orada da bir şey ayarlamalı mıyım?

hizmetim şöyle görünür:

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

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

Resmi örneği

Bu örnek (https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200) Aşağıdaki kullanması gerektiğini, yorum olarak gösterir, ancak startServiceInForeground yok ...

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); 
} 

Edit

Benim bildirim şimdiye kadar API < 26 ile android cihazların binlerce üzerinde çalışıyor hangi bu gibi oluşturulur:

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(); 
} 

cevap

11

Uygulamanız için Bildirim Kanal tanımlamak gerekebilir. Günlüğü kontrol et, uyarı olmalı. Kontrol için this kontrol edin

Size bir örnek vereceğim, kotin olurdu. İlk olarak, böyle bir sınıf yapın. Uygulamanızın başlangıcında bir kez createMainNotificationChannel() öğesini çağırmanız gerekir. Bu

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

gibi katma bağlantı zaten oldukça iyi util

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) } } 

Sonra, teşekkürler kullanabilirsiniz. Yine de bir soru: Bu değişiklik, android oreo'da artık bir ön plan servisi için bir bildirim göstermeye gerek olmadığı anlamına mı geliyor? İnsanlar bana benim app bildirimi olmadan android oreo üzerinde çalışıyor bana söylediler ... – prom85

+1

eghmmm ... Aslında bildirim olmadan çalışamaz. Arka planda çalışmalara, Android'de etkinlik veya bildirim gibi görünür bileşenler olmadan izin verilmez. Hizmetinizi görünür bileşen sistemi olmadan çalıştırırsanız, bir süre sonra onu öldürür. Arka plan işi yapmak istiyorsanız servis için bildirim almanız gerekir. Belirli bir süre içinde arka planda bir şeyler çalıştırmanız gerekiyorsa, [firebase iş dağıtıcısı] 'nı kontrol etmeniz gerekir (https://github.com/firebase/firebase-jobdispatcher-android). Zamanlanmış hizmeti çalıştırmanızı sağlar. –

+0

Ben 'WindowManager' üzerinde UI elemanları ile bir kenar çubuğu uygulaması var ve ben şimdiye kadar kullandığım görünmez bildirim ile çalışmak gibi görünüyor (görünmez bildirim bir kanal olmadan birdir). Birkaç gün içinde herhangi bir şikayet gelmedi. Emulator, 5 saniye içinde hizmetimi durdurmuyor – prom85

İlgili konular