不要根据自定义数据显示远程通知

时间:2016-11-04 18:55:37

标签: ios swift push-notification apple-push-notifications onesignal

我想为用户提供启用/禁用某些类型通知的选项。例如,打开私人消息的推送通知,但关闭公共帖子。我想知道在收到APN之后是否可以在客户端执行此操作(在didReceiveRemoteNotification中),我不确定在调用didReceiveRemoteNotification之前是否已显示通知。

目前我正在使用OneSignal发送推送通知,并使用content-available为true发送它们(1)。当应用程序在后台时,这会正确触发didReceiveRemoteNotification。根据我发送的数据,我想显示或不显示任何横幅推送通知。

这是我到目前为止所尝试的内容,我能够获得我需要的数据但是我无法弄清楚如何告诉iDevice根本不显示它:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("****this happened****")
    print("\(userInfo)")
    if let customData = userInfo["custom"] as? [String: Any] {
        if let additionalData = customData["a"] as? [String: Any] {
            if let notificationType = additionalData["notification_type"] as? String {
                if notificationType == "follow" {
                    print("do not want to display")
                    if var apsData = userInfo["aps"] as? [String: Any] {
                        apsData.removeValue(forKey: "content-available")
                    }
                } else {
                    print("dispaying this notification")
                }
            }
        }
    }
}

我想到的另一种方法是将用户的通知设置存储到我的数据库中,以及当用户发送推送通知以检查那些用户是否应该发送推送时。我想知道是否有可能在不添加更多数据库查询的情况下解决这个问题。

编辑:我也使用OneSignal.initWithLaunchOptions并注意到在handleNotificationReceived中的didReceiveRemoteNotification之前调用它,我想知道是否可以在这里停止推送通知

if receivedNotification?.payload != nil {
     if receivedNotification?.payload.additionalData != nil {
         if let data = receivedNotification?.payload.additionalData["notification_type"] as? String {
             if data == "follow" {
                print("do not display this")
             }
         }
     }
 }

1 个答案:

答案 0 :(得分:0)

相反,我建议在您的服务器上进行过滤,因为向所有设备发送通知(只是为了省略它们的显示),包括:当用户刷掉应用程序时,额外的电量消耗和mutable-content通知不起作用。

您可以使用include_player_ids来激活您的UNNotificationServiceExtension,但这仅适用于iOS 10,您仍然需要考虑电池。

您可以通过拨打IdsAvailable来获取设备的OneSignal播放器ID,您可以将其存储在后端。然后,您可以使用create notification REST API call上的uppercase仅定位应该收到通知的用户。