Android: несколько уведомлений в виде одного списка в строке состояния
Я пытаюсь Notify
пользователь на основе некоторых критериев. Multiple Notifications
показываются в Status Bar
но я хочу Group the notification in single notification
и когда пользователь нажимает на Status Bar
Я хочу получить все уведомления в этой группе. Это возможно? Или я должен поддерживать PendingIntents
из этих уведомлений? Любая помощь будет оценена. Например, если дни рождения двух друзей приходят в один и тот же день, то должны отображаться 2 уведомления. Я хочу объединить эти уведомления, т.е. вместо 2-х уведомлений в строке состояния, я хочу одно, когда пользователь нажимает на него, оно должно иметь информацию о 2-х уведомлениях. Является ли это возможным?
Пожалуйста, смотрите код ниже для отображения уведомлений.
public void displayNotification(BirthdayDetail detail)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.context);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(detail.getContactName());
builder.setContentText(detail.getContactBirthDate());
Intent resultIntent = new Intent(this.context, NotificationView.class);
resultIntent.putExtra("name", detail.getContactName());
resultIntent.putExtra("birthdate", detail.getContactBDate());
resultIntent.putExtra("picture_path", detail.getPicturePath());
resultIntent.putExtra("isContact", detail.isFromContact());
resultIntent.putExtra("notificationId", notificationId);
if(detail.isFromContact())
{
resultIntent.putExtra("phone_number", detail.getPhoneNumber());
}
PendingIntent resultPendingIntent = PendingIntent.getActivity(this.context, requestCode++,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager
= (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());
notificationId++;
}
1 ответ
Если вам нужно отправить уведомление несколько раз для одного и того же типа события, вам следует избегать создания совершенно нового уведомления. Вместо этого вам следует рассмотреть возможность обновления предыдущего уведомления, либо изменив некоторые из его значений, либо добавив к нему, либо и то, и другое.
Вы можете использовать что-то вроде:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
Источник: http://developer.android.com/training/notify-user/managing.html