我想給用戶啓用/禁用某些類型的通知的選項。例如,打開私人郵件的推送通知,但關閉公共帖子。我想知道是否可以在接收到APN(在didReceiveRemoteNotification中)後在客戶端執行此操作,我不確定在調用didReceiveRemoteNotification之前是否已經顯示通知。不要顯示基於自定義數據的遠程通知
目前我正在使用OneSign發送推送通知,並且正在使用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,發現這個被didReceiveRemoteNotification在handleNotificationReceived之前,我想知道是否有可能停在這裏的推送通知
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")
}
}
}
}
我有一種感覺,這可能是我不得不採取的解決方案,我希望Apple或OneSignal iOS SDK具有類似於Android的NotificationExtenderService的功能,那麼我就必須以這種方式實現它!謝謝! – Hellojeffy
@Hellojeffy我爲第二個選項添加了「mutable-content」。即使在Android上有'NotificationExtenderService',仍然推薦使用像iOS這樣的'include_player_ids',因爲這樣可以防止設備在第一時間被喚醒並保存在用戶的電池上。 – jkasten