我想更新通知数据,但我发现的唯一方法是启动一个具有相同 ID 的新通知数据。
问题是,如果原件已被取消,我不想提出一个新的。有没有办法判断通知是否可见或已取消?还是一种仅在通知存在时才更新通知的方法?
我想更新通知数据,但我发现的唯一方法是启动一个具有相同 ID 的新通知数据。
问题是,如果原件已被取消,我不想提出一个新的。有没有办法判断通知是否可见或已取消?还是一种仅在通知存在时才更新通知的方法?
如果您的应用程序至少API >= 23
可以使用此方法获取活动通知:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
if (notification.getId() == 100) {
// Do something.
}
}
这就是我解决它的方法:
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);
}
我认为您可以使用类的deleteIntentNotification
。
我记得在我的一个应用程序中,当通知被取消或通知托盘被清除时,我使用它来触发广播(自定义广播)。
deleteIntent
以下是在我自己的应用程序中证明是有益的替代方案:
基本上,您使用启动IntentService(或任何其他服务)的通知创建一个意图,并且onHandleIntent
您可以设置一个标志来指示通知是否处于活动状态。
您可以将此意图设置为在用户点击通知 ( contentIntent ) 和/或当用户从列表中清除它时 ( deleteIntent ) 时触发。
为了说明这一点,这是我在自己的应用程序中所做的。在构建我设置的通知时
Intent intent = new Intent(this, CleanupIntentService.class);
Notification n = NotificationCompat.Builder(context).setContentIntent(
PendingIntent.getActivity(this, 0, intent, 0)).build();
当通知被点击时,我CleanupIntentService
的启动,设置一个标志(在创建通知的服务中):
@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);
}
在我的情况下,我想在显示另一个通知之前检查一个通知是否已经显示。事实证明,有一种简单的方法可以做到这一点,而无需听取通知何时被删除或关闭.setAutoCancel(true)
on NotificationManagerCompat.Builder
。
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
}