java - 滑动关闭前台服务通知

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

即使我 setOnGoing(假); 它仍然没有工作。我遵循了这个线程解决方案:Make a notification from a foreground service cancelable once the service is not in foreground anymore

这是新的 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();

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

最佳答案

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

stopForeground(false);

并完全驳回通知;
stopForeground(true);

关于java - 滑动关闭前台服务通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49511119/

相关文章:

java - 从 servlet 获取 serviceContext

java - Java中codingBat的twoTwo谜语解答

java - 我应该用对象类型为我的变量添加后缀吗?

java - Android:如何点击我的通知直接转到特定屏幕?

android - java.lang.NoSuchMethodError : android. 应用程序通知$Builder.addAction

android - 在没有 AppCompat 的情况下显示 Android O 及更高版本的通知

java - 在 Java 中序列化监听器

java - Tomcat REST API 路径大小

android - Android Studio 中 IdeaVim 的折叠/展开键盘映射

java - findviewbyid 给出意外的空指针异常