简体   繁体   中英

How to ignore a remote/push notification while app is on background/terminated

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.

如果您从推送通知日期的正文中删除键“ content_available”:true”,则当应用程序处于后台或终止状态时,不会收到推送通知。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM