隐藏某些传入通知 - OneSignal
Hiding certain incoming notifications - OneSignal
我正在开发一个聊天应用程序,使用 xCode、Swift、Firestore 和 OneSignal 进行通知。一切都已设置好,我正在相应地接收通知,但我想根据某些条件过滤(隐藏、显示)一些通知。例如,如果用户当前正在与该人聊天,我不希望向用户显示关于 "new message" 的推送通知,因为他们已经在当前聊天视图中看到了该消息。有什么方法可以用 OneSignal 实现吗?
您可以这样更改 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
}
我正在开发一个聊天应用程序,使用 xCode、Swift、Firestore 和 OneSignal 进行通知。一切都已设置好,我正在相应地接收通知,但我想根据某些条件过滤(隐藏、显示)一些通知。例如,如果用户当前正在与该人聊天,我不希望向用户显示关于 "new message" 的推送通知,因为他们已经在当前聊天视图中看到了该消息。有什么方法可以用 OneSignal 实现吗?
您可以这样更改 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
}