【问题标题】:Do not display a remote notification based on custom data不显示基于自定义数据的远程通知
【发布时间】:2016-11-04 18:55:36
【问题描述】:

我想为用户提供启用/禁用某些类型通知的选项。例如,打开私人消息的推送通知,但关闭公共帖子。我想知道是否可以在收到 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")
             }
         }
     }
 }

【问题讨论】:

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


    【解决方案1】:

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

    您可以改用mutable-content,它会触发您的UNNotificationServiceExtension,但这仅适用于iOS 10,您仍然需要考虑电池。

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

    【讨论】:

    • 我觉得这可能是我必须采用的解决方案,我希望 Apple 或 OneSignal iOS SDK 有类似于他们的 Android 的 NotificationExtenderService 的东西,我想我必须实现它那么方法!谢谢!
    • @Hellojeffy 我在第二个选项的答案中添加了mutable-content。即使在 Android 上使用NotificationExtenderService,仍然建议像 iOS 一样使用include_player_ids,因为这样可以防止设备从一开始就被唤醒并节省用户的电池电量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    相关资源
    最近更新 更多