You can send data notifications that will be received both in fore- and background. In iOS they are processed by the delegate method application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
, after processing you can decide there to create a push for the user or not.
@update
For receiving data message (not push) on iOS lower than 10
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
handleNotification(data: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
on iOS10
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
handleNotification(data: remoteMessage.appData)
}
And then I just check if custom fields have data I need and use NotificationCenter to create local notification
fileprivate func handleNotification(data: [AnyHashable : Any]) {
guard let topic = data["topic"] as? String else { return }
if data["receiver"] as? String == UserManager.shared.current(Profile.self)?.uuid && topic == "coupons" {
displayNotification(data: data)
}
}
fileprivate func displayNotification(data: [AnyHashable : Any]) {
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.title = data["notification_title"] as! String
content.body = data["notification_body"] as! String
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
let request = UNNotificationRequest.init(identifier: data["topic"] as! String, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
if currentNotification != nil {
UIApplication.shared.cancelLocalNotification(currentNotification!)
}
let notification = UILocalNotification()
notification.fireDate = Date()
notification.category = data["topic"] as? String
notification.alertBody = data["notification_body"] as? String
if #available(iOS 8.2, *) {
notification.alertTitle = data["notification_title"] as? String
}
currentNotification = notification
UIApplication.shared.scheduleLocalNotification(currentNotification!)
}
}
Just remember this works with data notification not push notification, but in Firebase you are able to send both types.