0

I am developping an instant messaging app, and want to show a notification when a message is received .

Upon first message, the notification pops up and there is an icon in the status bar, as expected.

After the first message, I update the notification which reflects in the status bar, but the notification does not pop up a second time.

I've tried several things such as changing the notification ID, but that does not work as I need to have only one notification, that is updated and shown, not several notifications.

I am stuck now, please advise.

Edit: Added code.

if (messageNotifications.isEmpty()) {
        return null;
    }

    int messageCount = 0;

    for (MessageNotification messageNotification : messageNotifications) {
        messageCount += messageNotification.getCount();
    }

    MessageNotification message = messageNotifications.get(messageNotifications.size() - 1);

    boolean showText = false;

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application);
    notificationBuilder.setContentTitle("Secure Messenger");
    notificationBuilder.setContentText("Secure Message");

    notificationBuilder.setTicker(getText(message, showText));

    notificationBuilder.setSmallIcon(getSmallIcon());
    notificationBuilder.setLargeIcon(getLargeIcon(message));

    notificationBuilder.setColor(ColorManager.getInstance().getAccountPainter().getAccountMainColor(message.getAccount()));
    notificationBuilder.setStyle(getStyle(message, messageCount, showText));

    notificationBuilder.setContentIntent(getIntent(message));

    notificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
    notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
    notificationBuilder.setOnlyAlertOnce(false);

    NotificationManager.addEffects(notificationBuilder, messageItem);

    return notificationBuilder.build();

private PendingIntent getIntent(MessageNotification message) {
    Intent i = new Intent();
    i.setAction(Constants.ACTION_SPECIFIC_CHAT);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Notification.FLAG_ONGOING_EVENT);
    i.addFlags(Notification.FLAG_NO_CLEAR);

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("com.xabber.android.data.user", message.getUser().toString());
    i.putExtra("com.xabber.android.data.account", message.getAccount().toString());
    i.setData((Uri.parse("custom://" + System.currentTimeMillis())));


    return PendingIntent.getActivity(application, UNIQUE_REQUEST_CODE++, i, PendingIntent.FLAG_CANCEL_CURRENT);
}
Tom
  • 305
  • 5
  • 24
  • kindly post some screenshots and code to say what you've tried – Ankit Sharma Jul 28 '17 at 03:08
  • Have you tried deleting the notification and adding it again? Or alternatively, recall the first one with, say, id 1 and when you add it again try a different id. https://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar. My understanding is that when you reuse an id you're editing an existing notification. – Fabio Jul 28 '17 at 03:30
  • Hi Fabio, yes I tried cancelling all notifications but it didn't work. – Tom Jul 28 '17 at 04:03

0 Answers0