【发布时间】: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