0

我想給用戶啓用/禁用某些類型的通知的選項。例如,打開私人郵件的推送通知,但關閉公共帖子。我想知道是否可以在接收到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") 
      } 
     } 
    } 
} 

回答

0

相反,我建議你的服務器上過濾稱爲因爲向所有設備發送通知(只是爲了省略它們而不被顯示)具有下面的側面:額外的電池消耗和content-available通知不起作用,當用戶劃掉應用程序。

您可以使用mutable-content來代替UNNotificationServiceExtension,但這隻適用於iOS 10,您仍然需要考慮電池。

您可以通過撥打IdsAvailable來獲得設備的OneSignal播放器ID,您可以在此將其存儲在您的後端。然後,您可以在create notification REST API call上使用include_player_ids僅定位應接收通知的用戶。

+0

我有一種感覺,這可能是我不得不採取的解決方案,我希望Apple或OneSignal iOS SDK具有類似於Android的NotificationExtenderService的功能,那麼我就必須以這種方式實現它!謝謝! – Hellojeffy

+0

@Hellojeffy我爲第二個選項添加了「mutable-content」。即使在Android上有'NotificationExtenderService',仍然推薦使用像iOS這樣的'include_player_ids',因爲這樣可以防止設備在第一時間被喚醒並保存在用戶的電池上。 – jkasten