隐藏某些传入通知-OneSignal

问题描述 投票:1回答:1

我正在使用xCode,Swift,Firestore和OneSignal开发用于通知的聊天应用程序。一切都已设置好,我正在相应地接收通知,但是我想根据某些条件过滤(隐藏,显示)一些通知。例如,如果用户当前正在与该人聊天,那么我不希望有关“新消息”的推送通知显示给用户,因为他们已经在当前的聊天视图中看到了该消息。我可以用OneSignal达到什么目的吗?

ios google-cloud-firestore onesignal
1个回答
0
投票

您可以这样更改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 
}
© www.soinside.com 2019 - 2024. All rights reserved.