Скрытие определенных входящих уведомлений - OneSignal - PullRequest
1 голос
/ 29 марта 2020

Я разрабатываю приложение чата, используя xCode, Swift, Firestore и OneSignal для уведомлений. Все настроено, и я получаю уведомления соответственно, но я хочу отфильтровать (скрыть, показать) некоторые уведомления на основе некоторых условий. Например, я не хочу, чтобы уведомление pu sh о «новом сообщении» отображалось пользователю, если он в данный момент общается с этим человеком, поскольку он уже видит сообщение в текущем представлении чата. Есть ли способ, которым я могу достичь этого с OneSignal?

1 Ответ

0 голосов
/ 30 марта 2020

Вы можете изменить appDelegate следующим образом:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in

  print("Received Notification: \(notification!.payload.notificationID)")
  NotificationCenter.default.post(name: "ReceivedNotification", object: self, userInfo: notification!.payload) // post notification to view controller
}

let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
  // This block gets called when the user reacts to a notification received

}

let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,
  kOSSettingsKeyInAppLaunchURL: true]

OneSignal.initWithLaunchOptions(launchOptions, 
  appId: "YOUR_ONESIGNAL_APP_ID", 
  handleNotificationReceived: notificationReceivedBlock, 
  handleNotificationAction: notificationOpenedBlock, 
  settings: onesignalInitSettings)

OneSignal.inFocusDisplayType = OSNotificationDisplayType.none //Notification is silent and not shown

return true
}

И ваш контроллер просмотра чата:

override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
       NotificationCenter.default.addObserver(self,
       selector: #selector(receivedNotification(_:)),
       name: "ReceivedNotification", object: nil)
   }
override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: "ReceivedNotification", object: nil)
}

@objc func receivedNotification(_ notification: Notification) {
    let notificationPayload =  notification.userInfo
    //check the message belongs to this room then if you want show your local notification , if you want do nothing 
}
...