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

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

4

2 回答 2

2

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

stopForeground(false);

并完全驳回通知;

stopForeground(true);
于 2018-11-13T07:01:36.990 回答
0

这段代码完全适合我

stopForeground(true);
于 2020-06-25T09:46:27.103 回答