@ -36,12 +36,12 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
// A b o r t i f t h e m a i n a p p i s r u n n i n g
guard ! ( UserDefaults . sharedLokiProject ? [ . isMainAppActive ] ) . defaulting ( to : false ) else {
Log . info ( " didReceive called while main app running. " )
return self . completeSilenty ( isMainAppAndActive: true )
return self . completeSilenty ( handledNotification: false , isMainAppAndActive: true )
}
guard let notificationContent = request . content . mutableCopy ( ) as ? UNMutableNotificationContent else {
Log . info ( " didReceive called with no content. " )
return self . completeSilenty ( )
return self . completeSilenty ( handledNotification : false )
}
Log . info ( " didReceive called. " )
@ -69,7 +69,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
guard metadata . accountId = = getUserHexEncodedPublicKey ( using : dependencies ) else {
guard ! isPerformingResetup else {
Log . error ( " Received notification for an accountId that isn't the current user, resetup failed. " )
return self . completeSilenty ( )
return self . completeSilenty ( handledNotification : false )
}
Log . warn ( " Received notification for an accountId that isn't the current user, attempting to resetup. " )
@ -90,9 +90,15 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
// t h e s e w i l l m o s t c o m m o n l y b e c a l l o r c o n f i g m e s s a g e s )
case . successTooLong :
Log . info ( " Received too long notification for namespace: \( metadata . namespace ) , dataLength: \( metadata . dataLength ) . " )
return self . completeSilenty ( )
return self . completeSilenty ( handledNotification : false )
case . legacyForceSilent , . failureNoContent : return self . completeSilenty ( )
case . legacyForceSilent :
Log . info ( " Ignoring non-group legacy notification. " )
return self . completeSilenty ( handledNotification : false )
case . failureNoContent :
Log . warn ( " Failed due to missing notification content. " )
return self . completeSilenty ( handledNotification : false )
}
}
@ -189,6 +195,15 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
case ( true , false ) :
try MessageReceiver . insertCallInfoMessage ( db , for : callMessage )
// P e r f o r m a n y r e q u i r e d p o s t - h a n d l i n g l o g i c
try MessageReceiver . postHandleMessage (
db ,
threadId : threadId ,
threadVariant : threadVariant ,
message : messageInfo . message
)
return self ? . handleSuccessForIncomingCall ( db , for : callMessage )
}
@ -213,8 +228,8 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
}
db . afterNextTransaction (
onCommit : { _ in self ? . completeSilenty ( ) } ,
onRollback : { _ in self ? . completeSilenty ( ) }
onCommit : { _ in self ? . completeSilenty ( handledNotification : true ) } ,
onRollback : { _ in self ? . completeSilenty ( handledNotification : false ) }
)
}
catch {
@ -222,9 +237,22 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
// t h e e r r o r o u t s i d e o f t h e d a t a b a s e
let handleError = {
switch error {
case MessageReceiverError . invalidGroupPublicKey , MessageReceiverError . noGroupKeyPair ,
MessageReceiverError . outdatedMessage , NotificationError . ignorableMessage :
self ? . completeSilenty ( )
case MessageReceiverError . noGroupKeyPair :
Log . warn ( " Failed due to having no legacy group decryption keys. " )
self ? . completeSilenty ( handledNotification : false )
case MessageReceiverError . outdatedMessage :
Log . info ( " Ignoring notification for already seen message. " )
self ? . completeSilenty ( handledNotification : false )
case NotificationError . ignorableMessage :
Log . info ( " Ignoring message which requires no notification. " )
self ? . completeSilenty ( handledNotification : false )
case MessageReceiverError . duplicateMessage , MessageReceiverError . duplicateControlMessage ,
MessageReceiverError . duplicateMessageNewSnode :
Log . info ( " Ignoring duplicate message (probably received it just before going to the background). " )
self ? . completeSilenty ( handledNotification : false )
case NotificationError . messageProcessing :
self ? . handleFailure ( for : notificationContent , error : . messageProcessing )
@ -282,7 +310,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
switch result {
case . failure ( let error ) :
Log . error ( " Failed to complete migrations: \( error ) . " )
self ? . completeSilenty ( )
self ? . completeSilenty ( handledNotification : false )
case . success :
// W e s h o u l d n e v e r r e c e i v e a n o n - v o i p n o t i f i c a t i o n o n a n a p p t h a t d o e s n ' t s u p p o r t
@ -292,7 +320,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
// a n d d o n ' t d i s t u r b t h e u s e r . M e s s a g e s w i l l b e p r o c e s s e d w h e n t h e y o p e n t h e a p p .
guard Storage . shared [ . isReadyForAppExtensions ] else {
Log . error ( " Not ready for extensions. " )
self ? . completeSilenty ( )
self ? . completeSilenty ( handledNotification : false )
return
}
@ -317,7 +345,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
// A p p i s n ' t r e a d y u n t i l s t o r a g e i s r e a d y A N D a l l v e r s i o n m i g r a t i o n s a r e c o m p l e t e .
guard Storage . shared . isValid else {
Log . error ( " Storage invalid. " )
return self . completeSilenty ( )
return self . completeSilenty ( handledNotification : false )
}
// I f t h e a p p w a s n ' t r e a d y t h e n m a r k i t a s r e a d y n o w
@ -363,10 +391,10 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
// C a l l e d j u s t b e f o r e t h e e x t e n s i o n w i l l b e t e r m i n a t e d b y t h e s y s t e m .
// U s e t h i s a s a n o p p o r t u n i t y t o d e l i v e r y o u r " b e s t a t t e m p t " a t m o d i f i e d c o n t e n t , o t h e r w i s e t h e o r i g i n a l p u s h p a y l o a d w i l l b e u s e d .
Log . warn ( " Execution time expired. " )
completeSilenty ( )
completeSilenty ( handledNotification : false )
}
private func completeSilenty ( isMainAppAndActive: Bool = false ) {
private func completeSilenty ( handledNotification: Bool , isMainAppAndActive: Bool = false ) {
// E n s u r e w e o n l y r u n t h i s o n c e
guard
hasCompleted . mutate ( { hasCompleted in
@ -382,7 +410,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
. map { NSNumber ( value : $0 ) }
. defaulting ( to : NSNumber ( value : 0 ) )
Log . info ( " Complete silently." )
Log . info ( handledNotification ? " Complete d after handling notification." : " Completed silently." )
if ! isMainAppAndActive {
Storage . suspendDatabaseAccess ( )
}
@ -412,7 +440,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
else {
Log . info ( " Successfully notified main app of call message. " )
UserDefaults . sharedLokiProject ? [ . lastCallPreOffer ] = Date ( )
self ? . completeSilenty ( )
self ? . completeSilenty ( handledNotification : true )
}
}
}
@ -457,8 +485,8 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension
Log . info ( " Add remote notification request. " )
db . afterNextTransaction (
onCommit : { [ weak self ] _ in self ? . completeSilenty ( ) } ,
onRollback : { [ weak self ] _ in self ? . completeSilenty ( ) }
onCommit : { [ weak self ] _ in self ? . completeSilenty ( handledNotification : true ) } ,
onRollback : { [ weak self ] _ in self ? . completeSilenty ( handledNotification : false ) }
)
}