Подтвердить что ты не робот

Служба Oreo - Foreground не показывает уведомление переднего плана

До сих пор я применил мой код для использования ContextCompat.startForegroundService(context, intentService); чтобы начать мой сервис. Таким образом, он работает на android <26 и на android 26 (Oreo).

Я все еще вижу разницу, в android oreo я не вижу свое собственное предварительное уведомление (я вижу только уведомление "приложение работает в фоновом режиме"). Нужно ли мне что-то корректировать?

Моя служба выглядит так:

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

редактировать

Мое уведомление создано таким образом, которое работает над тысячами Android-устройств с API <26 до сих пор:

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

Ответ 1

Возможно, вам потребуется определить канал уведомлений для вашего приложения. Проверьте журнал, должно быть предупреждение. Проверьте это для ознакомления

Я приведу вам пример, это будет в котине. Сначала сделайте такой класс. Вам нужно будет вызвать 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)
    }
}

Тогда вы можете использовать так, как это

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

Ответ 2

Код в Котлине

 private fun customNotification(title: String,notificationID: Int) {

                val intent = Intent(this, MainActivity::class.java)
                val pendingIntent = PendingIntent.getActivity(this, 0 /* request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)


                val builder = NotificationCompat.Builder(this, title)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentText("Notification description")
                        .setSmallIcon(R.drawable.ic_mark_map)
                        .setContentIntent(pendingIntent)
                        .setOnlyAlertOnce(true)

                val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val channel = NotificationChannel(title,
                            "Channel human readable title",
                            NotificationManager.IMPORTANCE_DEFAULT)
                    mNotificationManager.createNotificationChannel(channel)
                }

                 val notification = builder.build()
                 startForeground(notificationID, notification)
            }

Использование:

 customNotification("Title",101)

Ответ 3

  // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        getString(R.string.app_name),
                        NotificationManager.IMPORTANCE_HIGH);

                AudioAttributes attributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();


                channel.setDescription(data.get("body"));
                channel.enableLights(true);
                channel.enableVibration(true);
                channel.setSound(SoundUri, attributes); // This is IMPORTANT

                notificationManager.createNotificationChannel(channel);


            }