繁体   English   中英

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

[英]Swipe to dismiss notification on foreground service

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

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

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

stopForeground(false);

并完全取消通知;

stopForeground(true);

这段代码完全适合我

stopForeground(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM