Можно ли проверить, видно или отменено уведомление?
Я хотел бы обновить данные уведомления, но единственный способ, которым я нашел, - запустить новый с тем же идентификатором.
Проблема в том, что я не хочу поднять новый, если оригинал отменен.
Есть ли способ узнать, видно или отменено уведомление? Или способ обновления уведомления, только если он существует?
Ответы
Ответ 1
Вот как я это решил:
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);
}
Ответ 2
Если у вашего приложения минимум API >= 23
, вы можете использовать этот метод для получения активного уведомления:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
if (notification.getId() == 100) {
// Do something.
}
}
Ответ 3
Я думаю, вы можете использовать deleteIntent класса Notification
.
Я помню, что в одном из моих приложений я использую использование для рассылки широковещательной рассылки (настраиваемая широковещательная рассылка), когда уведомление отменено или лоток уведомлений очищен.
Ответ 4
Альтернативой 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);
}
Ответ 5
В моей ситуации я хотел проверить, показывалось ли уже Уведомление, прежде чем показывать другое. И оказывается, что существует простой способ сделать это без прослушивания, когда Уведомление было удалено или отклонено с помощью .setAutoCancel(true)
на 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)
}