Updated 26 May 2018
iOS App is running in 2 mode either the user is interacting with app (foreground) or minimize the app (background). if we are talking  about the firebase push notification then we have to handle by other way because iOS 10 to above provide the custom local notification for handling this type of issue when app is running in foreground.
Here I have taken the example of Firebase Push Notifaction.
1: First Set up firebase Push notification in App.
2: Open Appdelegate.swift File:
write this Code for checking the App is running on iOS 10 to above or less .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) FirebaseApp.configure() UNUserNotificationCenter.current().delegate = self Messaging.messaging().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() return true } |
3:Â It will Check weather your app is running in iOS 10 to above or less if iOS app is running on iOS 10 or above then it will assigned the local push notification for handle the foreground app.
4: Now return the local push notification type like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@available(iOS 10, *) extension AppDelegate : UNUserNotificationCenterDelegate { // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo // Print full message. print(userInfo) // Change this to your preferred presentation option completionHandler(UNNotificationPresentationOptions.alert) } } |
Note: UNNotificationPresentationOptions.alert – it will return a alert.
5: It will work when your app is running on foreground and someone send the notification .
6: Now we have to handle the Tap also for this write this function
1 2 3 4 5 6 7 8 9 10 11 |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo // Print full message. print("tap on on forground app",userInfo) completionHandler() } |
Note: This method will call when the user tap on foreground Notification.
7: Fire base also provide the other function for iOS 10 to above if token expire , for this write this method to handle this type of case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
extension AppDelegate : MessagingDelegate { // [START refresh_token] func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase registration token: \(fcmToken)") Messaging.messaging().subscribe(toTopic: "/topics/nutriewell_live") Messaging.messaging().shouldEstablishDirectChannel = true connectToFcm() // TODO: If necessary send token to application server. // Note: This callback is fired at each app startup and whenever a new token is generated. } // [END refresh_token] // [START ios_10_data_message] // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground. // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { print("Received data message: \(remoteMessage.appData)") } // [END ios_10_data_message] } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
is there any way to show a notification to the user when the app is in the foreground for iOS 10 and below?