Sujith S Manjavana
Sujith S Manjavana

Reputation: 1596

Handling race condition in Firebase messaging service

I'm using the following code to display chat notifications in my android app.

                //copy old notification (if any)
                Notification oldNotification = getActiveNotification(notificationId);
                NotificationCompat.MessagingStyle messagingStyle = null;
                if (oldNotification != null) {
                    messagingStyle = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(oldNotification);
                }


                Person you = new Person.Builder().setName("You").setIcon(IconCompat.createWithResource(this, android.R.color.transparent)).build();
                if (messagingStyle == null) {
                    messagingStyle = new NotificationCompat.MessagingStyle(you);
                    messagingStyle.setConversationTitle(senderName);
                    messagingStyle.setGroupConversation(false);
                }
                //add new message
                NotificationCompat.MessagingStyle.Message notificationMessage;
                if (fromMe) {
                    notificationMessage = new NotificationCompat.MessagingStyle.Message(msg, System.currentTimeMillis(), you);
                } else {
                    IconCompat tIcon = null;
                    if (dp != null) tIcon = IconCompat.createWithBitmap(dp);
                    Person them = new Person.Builder().setIcon(tIcon).setName(senderName).build();
                    notificationMessage = new NotificationCompat.MessagingStyle.Message(msg, System.currentTimeMillis(), them);
                }
                messagingStyle.addMessage(notificationMessage);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId).setDefaults(Notification.DEFAULT_ALL).setContentTitle(senderName).setSmallIcon(R.drawable.ic_buddy_request_notification).setAutoCancel(true).setStyle(messagingStyle).setLargeIcon(dp).setColorized(true).setColor(getColor(R.color.message)).setContentIntent(activityLaunchIntent).addAction(replyAction).setSound(soundUri).setCategory(Notification.CATEGORY_MESSAGE).setPriority(NotificationCompat.PRIORITY_HIGH);

             
                Notification myNotification = mBuilder.build();
                if (ActivityCompat.checkSelfPermission(NotificationHelper.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                managerCompat.notify(notificationId, myNotification);

   public Notification getActiveNotification(int notificationId) {
        try {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();
            for (StatusBarNotification notification : barNotifications) {
                if (notification.getId() == notificationId) {
                    return notification.getNotification();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

It works fine under normal conditions. But if the user is getting multiple messages at the same time, it creates a race condition, leading to some messages being skipped or overwritten. How can I fix it efficiently? Thanks in advance.

Upvotes: 1

Views: 127

Answers (0)

Related Questions