1

Technologies Used:

To give you a background here's the data I'm sending via FCM on an api server

{

    "priority": "high",
    "content_available": true,
    "alert": true,
    "sound": true,
    "registration_ids": [
         "<SOME_ID_HERE>", 
         "<SOME_ID_HERE>"
    ],
    "notification": {
        "title": "SOME TITLE",
        "body": "SOME BODY"
    },
    "data": {
        "some_key": "some_value",
        "some_key2": "some_value2"
    }
}

This is being received by the app correctly, and I'm correctly using these methods in a correct manner

application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage)

messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String)

userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

Is there a way to somehow ignore a push notification when the app is in background/terminated with this sample code?

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    switch application.applicationState {
    case .inactive, .background:
        // Tell Firebase that we recieved the Push Notification
        self.messaging.appDidReceiveMessage(userInfo)

        // `PushNotification` is a model that parses the `userInfo` dictionary
        guard let pushNotification: PushNotification = PushNotification(userInfo) else {
            return // end code execution cause push cannot be parsed
        }

        if pushNotification.isSomeKindOfThing {
            // ignore
        } else {
            // do nothing so it will show
        }
    case .active:
        // handle when application is active
    }
}

What I'm aiming for is that when I receive a push notification I'm able to either ignore it depending on what is in the "data" key.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Zonily Jame
  • 5,053
  • 3
  • 30
  • 56
  • You cant, when the application is inactive or terminated, OS handles receiving and delivering the notification to app. Use silent notifications for the notifications that you would want to ignore and normal ones which you want iOS to intimate user – Sandeep Bhandari Jan 22 '18 at 05:51
  • @SandeepBhandari yeah but the problem with that is a restructure of how our backend push sending works should be done. – Zonily Jame Jan 22 '18 at 06:10
  • Sorry am not sure what do u mean by that, if your question is how my server will get to know if the app is in background or foreground to construct payload accordingly,This approach is used by Quickblox chat framework, they have a API which they hit every time a client app goes to background, applicationDidEnterBackground use background session to ensure the successful completion of API call. That means u need to unnecessary info on ur server about each device state. If you ask is that worth I would say no. But than if thats your requirement go ahead n modify your backend code – Sandeep Bhandari Jan 22 '18 at 06:16
  • try to remove alert named dictionary or any object from push notification payload if exist and check. – KKRocks Jan 22 '18 at 06:17
  • 1
    @SandeepBhandari sorry here's a further explanation, what I meant by backend restructure was that if I am to use silent notifications the current backend api implementation for sending notifications needs to be changed, and yes i am aware of how to do that, but I'm currently looking for a way if possible to handle this via mobile side. – Zonily Jame Jan 22 '18 at 06:31
  • 1
    @zonily-jame : hmmmm interesting, sorry but am currently out of suggestions :P As far as I know its not possible but then there might be a way to do it so I as well will look forward for the answer posted here :) – Sandeep Bhandari Jan 22 '18 at 06:34
  • @ZonilyJame even i want to do same, i need to handle it from mobile side. Did you able to achieve it ? Please share solution. Thanks – Arshad Shaik Aug 01 '23 at 12:40

1 Answers1

0

If you remove the key "content_available": true" from the body of push notification date. Then push notifications will not received, when the app is in background or terminated.

Taimoor Suleman
  • 1,588
  • 14
  • 29