滑动以关闭前台服务的通知

Kro*_*rot 5 java android android-notifications foregroundnotification

我正在尝试关闭来自前台服务的通知,但尚未找到任何解决方案。我没有使用notificationManager.notify(...)但是startForeground(...)

我的NotificationCompat.Builder

Intent actionCancelNotification = new Intent(MusicPlayerApp.getAppContext(), NotificationReceiver.class);
actionCancelNotification.setAction(ACTION_DISMISS_NOTIFICATION);
actionCancelNotification.putExtra(PLAYBACK_NOTIFICATION_ID, PLAYBACK_NOTI_ID);

PendingIntent dismissNotiPendingIntent = PendingIntent.getBroadcast(MusicPlayerApp.getAppContext(), 0, actionCancelNotification, 0);

    //NotificationCompat.Builder
builder.setCustomBigContentView(getRemoteView())
       .setContentTitle("SongPlayback Notification")
       .setSmallIcon(R.drawable.ic_playback_notification_icon)
       .setLargeIcon(BitmapFactory.decodeResource(MusicPlayerApp.getAppContext().getResources(), R.drawable.ic_playback_notification_icon))
       .setContentText("this is content text")
       .setSubText("sub text")
       .setDeleteIntent(dismissNotiPendingIntent)
       .build();
Run Code Online (Sandbox Code Playgroud)

即使我setOnGoing(false); 它仍然没有工作。我遵循了这个线程解决方案:一旦服务不再处于前台,就可以取消来自前台服务的通知

这是新的NotificationCompat.Builder作为官方文档编写的:

//NotificationCompat.Builder
builder.setCustomBigContentView(getRemoteView())
       .setContentTitle("SongPlayback Notification")
       .setSmallIcon(R.drawable.ic_playback_notification_icon)
       .setLargeIcon(BitmapFactory.decodeResource(MusicPlayerApp.getAppContext().getResources(), R.drawable.ic_playback_notification_icon))
       .setContentText("this is content text")
       .setSubText("sub text")
       //this did not work too
       .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(MusicPlayerApp.getAppContext(), PlaybackStateCompat.ACTION_STOP)) //this d
       .build();
Run Code Online (Sandbox Code Playgroud)

我想知道是否有任何解决方案可以解决我的问题。谢谢

小智 3

您可以通过滑动来关闭通知;

stopForeground(false);
Run Code Online (Sandbox Code Playgroud)

并完全驳回该通知;

stopForeground(true);
Run Code Online (Sandbox Code Playgroud)