skip to Main Content

Is it possible to cancel a pushed notification before displaying it on the users phone ?

I have some custom logic which decides whether a notification needs to be displayed/appear . Is it possible for me to control this behaviour from the client side ios/android code ?

2

Answers


  1. Once a message is sent to Firebase Cloud Message, there is no way to cancel its delivery.

    If your message contains only a data element and no notification, displaying the message is handled by your application code – so in that case you may be able to suppress its display there.

    Login or Signup to reply.
  2. Although the best way is to handle this is to cancel it on backend side, you still can add UNNotificationServiceExtension and override the didReceive method:

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.receivedRequest = request;
        self.contentHandler = contentHandler
        self.content = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
    
        if let content = self.content {
            // I had to check something inside the push itself
            if let infoDictionary = content.userInfo {
                // Check something inside the push notification
                contentHandler(content)
                return
            }
        }
        // Otherwise, send an empty notification to the system and it will show nothing
        contentHandler(UNNotificationContent())
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search