问题描述
我想要的: 当用户手动删除我的应用的通知时,我想执行某个操作。
我是如何实现的:
我通过创建一个 PendingIntent
来实现这一点,该 PendingIntent.getbroadcast()
将使用 setDeleteIntent()
执行广播。然后我将此意图传递给 Notification.Builder
的 PendingIntent.getbroadcast()
方法。
这在以前的 Android 版本上运行良好,但在 Android 11 中,没有发送广播(或者至少我的接收器没有收到它)。我认为问题出在 setDeleteIntent()
(在我看来更有可能)或 sendbroadcast(new Intent(broADCAST_REMOVE_NOTIFICATION));
。
提示 1:
我确保我的 broadcastReceiver 工作正常:当我使用 // Set up intent to stop service when notification is removed
Intent intent = new Intent(broADCAST_REMOVE_NOTIFICATION);
PendingIntent deleteIntent = PendingIntent.getbroadcast(this.getApplicationContext(),intent,0);
// Create a new notification
mNotificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
// Hide the timestamp
.setShowWhen(false)
// Set the notification style
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
// Attach our MediaSession token
.setMediaSession(mediaSession.getSessionToken())
// Show our playback controls in the compat view
.setShowActionsInCompactView(0,1,2))
// Set the notification color
.setColor(getResources().getColor(R.color.colorAccent))
// Set the large and small icons
.setLargeIcon(notificationCover)
.setSmallIcon(R.drawable.ic_notification_new)
// Set notification content @R_544_4045@ion
.setContentText(mActiveAudio.getAlbumTitle())
.setContentTitle(audioTitle)
// Set the intent for the activity that is launched on click
.setContentIntent(launchIntent)
// Set intent that is launched on delete notificaiton
.setDeleteIntent(deleteIntent)
// Set the visibility for the lock screen
.setVisibility(VISIBILITY_PUBLIC)
// Make notification non-removable if the track is currently playing
.setongoing(title.equals(getString(R.string.button_pause)))
// Add playback actions
.addAction(R.drawable.ic_media_backward,getString(R.string.button_backward),playbackAction(3))
.addAction(notificationAction,title,play_pauseAction)
.addAction(R.drawable.ic_media_forward,getString(R.string.button_forward),playbackAction(2));
执行广播时,它工作得非常好。
提示 2: 我刚刚注意到,在我的应用程序的通知以前不可移动的状态下(在播放音频文件期间,我将播放器服务作为前台服务启动)它现在在 Android 11 中是可移动的。不确定这是否有关系虽然有上述问题。
我不知道是什么原因导致了 Android 11 和以前版本之间的这种行为差异。有人可以帮忙吗?
document.querySelector('td.t-c.dropdown').classList.add('hidden');
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)