如何在应用程序处于后台/终止时忽略远程/推送通知

时间:2018-01-22 05:47:11

标签: ios push-notification firebase-cloud-messaging swift3.2

使用的技术:

为了向您提供我在api服务器上通过FCM发送的数据的背景信息

{

    "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"
    }
}

应用正确收到了这个,我正确地正确使用这些方法

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)

当应用程序处于后台/使用此示例代码终止时,有没有办法以某种方式忽略推送通知?

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
    }
}

我的目标是,当我收到推送通知时,我可以根据"data"密钥中的内容忽略它。

1 个答案:

答案 0 :(得分:0)

如果您删除密钥&#34; content_available&#34;:true&#34;从推送通知日期的主体。当应用程序处于后台或终止时,将不会收到推送通知。