Android 是否可以检查通知是否可见或已取消?
我想更新通知数据,但我找到的唯一方法是使用相同的Id启动一个新的通知数据 问题是,如果原来的计划被取消了,我不想再提出一个新的计划。 有没有办法判断通知是否可见或已取消?或者只在通知存在时更新通知的方法?我认为您可以使用Android 是否可以检查通知是否可见或已取消?,android,notifications,Android,Notifications,我想更新通知数据,但我找到的唯一方法是使用相同的Id启动一个新的通知数据 问题是,如果原来的计划被取消了,我不想再提出一个新的计划。 有没有办法判断通知是否可见或已取消?或者只在通知存在时更新通知的方法?我认为您可以使用通知类 我记得在我的一个应用程序中,当通知被取消或通知托盘被清除时,我用它来触发广播(自定义广播)。我就是这样解决的: private boolean isNotificationVisible() { Intent notificationIntent = ne
通知类
我记得在我的一个应用程序中,当通知被取消或通知托盘被清除时,我用它来触发广播(自定义广播)。我就是这样解决的:
private boolean isNotificationVisible() {
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent test = PendingIntent.getActivity(context, MY_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);
return test != null;
}
以下是我生成通知的方式:
/**
* Issues a notification to inform the user that server has sent a message.
*/
private void generateNotification(String text) {
int icon = R.drawable.notifiaction_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, text, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, MY_ID, notificationIntent, 0);
notification.setLatestEventInfo(context, title, text, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL; //PendingIntent.FLAG_ONE_SHOT
notificationManager.notify(MY_ID, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate(); // If removed, onHandleIntent is not called
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
OtherService.setNotificationFlag(false);
}
deleteeintent
的另一种选择是以下内容,这些内容在我自己的应用程序中被证明是有益的:
基本上,您可以通过通知创建一个意图来启动(或任何其他服务),并在onHandleIntent
中设置一个标志,指示通知是否处于活动状态。
您可以将此意图设置为在用户点击通知()和/或用户从列表中清除通知()时触发
为了说明这一点,下面是我在自己的应用程序中所做的。在构建我设置的通知时
Intent intent = new Intent(this, CleanupIntentService.class);
Notification n = NotificationCompat.Builder(context).setContentIntent(
PendingIntent.getActivity(this, 0, intent, 0)).build();
点击通知时,将启动myCleanupIntentService
,设置一个标志(在创建通知的服务中):
如果您的应用程序具有最低API>=23
,则可以使用此方法获得:
在我的情况下,我想在显示另一个通知之前检查通知是否已经显示。事实证明,在NotificationManagerCompat.Builder
上使用.setAutoCancel(true)
删除或取消通知时,有一种简单的方法可以做到这一点,而无需监听
private val NOTIF_ID = 80085
private val CHANNEL_ID = "com.package.name.ClassName.WhatNotifycationDoes"
private lateinit var mNotificationManagerCompat: NotificationManagerCompat
private lateinit var mNotificationManager: NotificationManager // this is for creating Notification Channel in newer APIs
override fun onCreate() {
super.onCreate()
mNotificationManagerCompat = NotificationManagerCompat.from(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
mNotificationManager = getSystemService(NotificationManager::class.java)
showNotification()
startWatching()
}
private fun showNotification() {
val contentIntent = Intent(this, MainActivity::class.java)
.apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK }
val contentPendingIntent = PendingIntent.getActivity(this, 1, contentIntent, 0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.app_name)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, name, importance)
channel.description = getString(R.string.your_custom_description)
mNotificationManager.createNotificationChannel(channel)
}
val mNewStatusNotificationBuilder = NotificationCompat.from(this)
mNewStatusNotificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.simple_text))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(contentPendingIntent)
.setAutoCancel(true) // This dismisses the Notification when it is clicked
.setOnlyAlertOnce(true) //this is very important, it pops up the notification only once. Subsequent notify updates are muted. unless it is loaded again
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
mNewStatusNotificationBuilder.setSmallIcon(R.drawable.ic_notification)
notification = mNewStatusNotificationBuilder.build()
mNotificationManagerCompat.notify(NOTIF_ID, notification)
}
您可以在Kotlin中进行以下检查:
val mNotificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notifications: Array<StatusBarNotification> = mNotificationManager.activeNotifications
if(notifications.isNotEmpty())
{
//you don't have notifications
}
else
{
//you have notifications
}
val mNotificationManager:NotificationManager=getSystemService(Context.NOTIFICATION\u服务)作为NotificationManager
val通知:数组=mNotificationManager.activeNotifications
if(notifications.isNotEmpty())
{
//你没有通知
}
其他的
{
//你有通知吗
}
一定是出了什么问题。当我在我的onResume()
方法中写入if(isNotificationVisible())
时,我的通知应该通过以下方式取消:((NotificationManager)getSystemService(notification\u SERVICE))。取消(my\u ID)编码>然后再进行finish();startActivity(getIntent())代码>我得到一个循环,导致重新启动我的手机。所以这种方法应该谨慎对待……对我不起作用。这对我来说总是正确的。在Android 4.3中对我不起作用。在我取消通知后,甚至在我关闭了整个应用程序后,pendingent
仍然存在。只有重新启动手机才能摆脱它。另外,setLatestEventInfo()
似乎不再存在,所以我只使用了setContentIntent()
。刚刚在Android 7.1中测试了这个,并且isNotificationVisible()
总是返回false
。我在Android 5.0上不工作,测试总是空的,从哪里得到mNotificationManager?我添加了mNonificationManager
declration。不幸的是,它需要API 23。
val mNotificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notifications: Array<StatusBarNotification> = mNotificationManager.activeNotifications
if(notifications.isNotEmpty())
{
//you don't have notifications
}
else
{
//you have notifications
}