Как убрать уведомление при остановке сервиса? - PullRequest
0 голосов
/ 06 июня 2019

Код, который я сейчас использую, таков:

private void removeNotification(){
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
        notificationManager.deleteNotificationChannel(Constants.CHANNEL_ID.CHANNEL_ID);
    }
    if (notificationManager != null) {
        notificationManager.cancel(Constants.NOTIFICATION_ID.SERVICE_ID);
    }
    Log.i(TAG, "Notification removed!");
}

Но в чем разница между моим кодом и stopForeground(true);?Потому что он удаляет уведомление.

Какой из них использовать при уничтожении моей службы?

РЕДАКТИРОВАТЬ

Код уведомления:

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, Constants.CHANNEL_ID.CHANNEL_ID);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel =
                new NotificationChannel(
                        Constants.CHANNEL_ID.CHANNEL_ID,
                        "Media PlayBack",
                        NotificationManager.IMPORTANCE_LOW);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
        notificationBuilder.setChannelId(Constants.CHANNEL_ID.CHANNEL_ID);
    }
    notificationBuilder.setShowWhen(false)
            .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle().setMediaSession(mMediaSessionCompat.getSessionToken()).setShowActionsInCompactView(0, 1, 2))
            .setSmallIcon(android.R.drawable.stat_sys_headset).setContentTitle(activeSong.getTitle()).setContentText(activeSong.getArtist())
            .addAction(R.drawable.ic_action_prev_white, null, playbackAction(3))
            .addAction(notificationAction, null, play_pause_action).addAction(R.drawable.ic_action_next_white, null, playbackAction(2))
            .addAction(R.drawable.ic_close, null, playbackAction(4))
            .setColor(getResources().getColor(R.color.theme1));

    notificationBuilder.setOngoing(true);

    Notification notification = notificationBuilder.build();
    startForeground(Constants.NOTIFICATION_ID.SERVICE_ID, notification);

1 Ответ

3 голосов
/ 06 июня 2019

Код, который я сейчас использую, это

Не удаляйте канал.

Но в чем разница между моим кодом и stopForeground (true);?

stopForeground(true) работает только в том случае, если вы использовали startForeground().

Какой из них использовать при уничтожении моей службы?

Если вы использовали startForeground(), используйте stopForeground().Если вы подняли Notification напрямую, используя NotificationManager, cancel() Notification, используя NotificationManager.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...