java - 服务停止时如何删除通知?

标签 java android android-service android-notifications

我现在使用的代码是这样的:

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

最佳答案

Code i'm using now is this

不要删除 channel 。

But what is the difference between my code and stopForeground(true); ?

stopForeground(true) 仅在您使用 startForeground() 时才有效。

Which one should i use when my service gets destroyed?

如果您使用了startForeground(),请使用stopForeground()。如果您直接使用 NotificationManager 引发 Notification,则使用 NotificationManagercancel() Notification >.

关于java - 服务停止时如何删除通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56475942/

相关文章:

java - 如何从 Android 项目中删除 Kotlin 支持?

android - 如何在移动设备(iOS 和 Android)上使用 Flutter 将照片/视频移动到相册?

android - 无法启动服务 Intent ; Activity 中的 IntentService

Android 服务 - 从设置 App 停止服务

java - 将值添加到优先级队列时,值到底什么时候排序?

java - 在类里面存储日期

java - 如何在我的android项目中导入com.android.keyguard.KeyguardPatternView?

java - 当您必须替换许多 fragment 时,保存 fragment 状态的最佳实践方法是什么?

android - 显示来自服务的 Toast 通知

java - Java RabbitMQ 客户端中的ConfirmListener是否必须同步?