public void onNewMessage ( Peer peer , int sender , long date , ContentDescription description , boolean hasCurrentUserMention , int messagesCount , int dialogsCount ) { // Check if message already read to avoid incorrect notifications // for already read messages if ( date <= getLastReadDate ( peer ) ) { return ; } // If wee need to play "out-app" notification for message boolean isEnabled = isNotificationsEnabled ( peer , hasCurrentUserMention ) ; // Save to pending storage if ( isEnabled ) { List < PendingNotification > pendingNotifications = getNotifications ( ) ; PendingNotification pendingNotification = new PendingNotification ( peer , sender , date , description ) ; pendingNotifications . add ( pendingNotification ) ; pendingStorage . setMessagesCount ( messagesCount ) ; pendingStorage . setDialogsCount ( dialogsCount ) ; allPendingNotifications . add ( pendingNotification ) ; saveStorage ( ) ; } // Adding to paused cache if notifications paused // This peers will be released during call onNotificationsResumed if ( isNotificationsPaused ) { if ( ! notificationsDuringPause . containsKey ( peer ) ) { notificationsDuringPause . put ( peer , hasCurrentUserMention ) ; } else { if ( hasCurrentUserMention && ! notificationsDuringPause . get ( peer ) ) { notificationsDuringPause . put ( peer , true ) ; } } return ; } // Performing notification // // There are two separate modes of notifications: // First one is when app is visible, and second one when not. // // For in app notifications app need to be more careful as people already pay // full attention to app or may be particular chat. // For "in-app" notifications playing only sound effects for new messages. // // For "out-app" just playing default notifications. // // WARRING: This implementation is copied to onNotificationsResumed with small changes // to fit logic if ( isAppVisible ) { // If application is visible application play only sound effects and // doesn't show any "out-app" notifications if ( visiblePeer != null && visiblePeer . equals ( peer ) ) { // If message arrive to visible chat if ( isMobilePlatform ) { // Play sound effect on mobile playEffectIfEnabled ( ) ; } else { // Do nothing on desktop as it is very distracting // Most of the people use mobile messengers occasionally and // with disabled sounds and it doesn't make problems as much as on // non-mobile platforms } } else { if ( isMobilePlatform ) { // Don't play any sounds not in chat screen on mobile phone // We done this because we have unread badge in chat screen on mobile } else { // For desktop environments play sounds for all chats that // doesn't explicitly disabled if ( isEnabled ) { playEffectIfEnabled ( ) ; } } } } else { // If application is not visible play regular notification for enabled chats if ( isEnabled ) { showNotification ( ) ; } } }
Handling event about incoming notification