在前台服务中滑动以取消通知

5
我正在尝试关闭前台服务的通知,但是还没有找到任何解决方案。我没有使用 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();

即使我设置了`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();

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

你正在使用什么类型的服务?它是“START_NOT_STICKY”吗? - ADM
是的,它是START_NOT_STICKY - Krot
我在上面的帖子中提到了这个线程。我已经尝试了官方文档中提供的以下解决方案:https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice.html,但对我仍然没有用 :( - Krot
1
可能是重复的问题:如何取消前台服务使用通知(滑动取消)或清除所有通知?(原文链接:https://dev59.com/S2cs5IYBdhLWcg3waTRD) - Gautam
@Krot 你有解决方案吗? - mob_web_dev
2个回答

3

您可以通过滑动来使通知可消除;

stopForeground(false);

并完全关闭通知;
stopForeground(true);

0

这段代码对我来说完全有效

stopForeground(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接