Home > Net >  Cancel notification recieved from FireBase client side Android/IOS?
Cancel notification recieved from FireBase client side Android/IOS?

Time:08-10

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 ?

CodePudding user response:

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.

CodePudding user response:

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())
}
  • Related