iOS Swift Receive Push Notification When App in Foreground
Asked Answered
D

5

5

Here is the code that I have used to receive push Notification when app in foreground

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
     completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
}

But my problem is that my notification contains [NSObject : AnyObject] values. How to receive like background notification

Dextrous answered 27/12, 2016 at 7:1 Comment(6)
Do you want to access user info from Notification object?Kathe
@iOS_devloper yes sirDextrous
I am using "notification.request.content.userInfo" to get data. You can print this in above method.Windburn
You can use notification.request.content.userInfoKathe
@iOS_devloper If it solved the post, you should add that in as the answer (plus a few more details if you have them). Cheers!Parachronism
@Parachronism Thanks! Posted.Kathe
D
9

Add this code in AppDelegate.swift file.

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
}
Dextrous answered 21/4, 2017 at 6:55 Comment(9)
how to write this in iOS 8.0 @Kavin Kumar ArumugamTopsail
@DilipTiwari there is no changes in above code, it already supports iOS 8.0Dextrous
ok so it will show notification in foreground also @Kavin Kumar Arumugam ?Topsail
can you tell me your firebase version @DilipTiwariDextrous
i m receiving in ios 10 but not in ios 8 @Kavin Kumar ArumugamTopsail
can we talk in a group chat @kavin kumar arumugamTopsail
this is my code paste.ofcode.org/a3fEwUmYi7bQPYG7WHHgjA @Kavin Kumar ArumugamTopsail
Little busy now will see and update your code by evening @DilipTiwariDextrous
Let us continue this discussion in chat.Topsail
R
1
 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

   if (application.applicationState == 0) // active --- forground
    {
       // publish your notification message
    }

}
Racism answered 27/12, 2016 at 7:9 Comment(0)
B
1

your app won't be able to run any code unless the user open the push notification, push notifications are handled by the OS and your app have no control on them while it's not active or in the background you should take a look https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html

Bortman answered 27/12, 2016 at 7:15 Comment(0)
K
1

UNNOtification has property request (UNNotificationRequest), you can use that to get user info.

use following to access user info from UNNotification:

let userinfo =  notification.request.content.userInfo
Kathe answered 28/12, 2016 at 7:41 Comment(0)
M
0
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
    debugPrint("Received when is visible... :): \(userInfo)")
    

    guard let aps = userInfo["aps"] as? [String: AnyObject] else {
        completionHandler(.failed)
        return
      }
    
    let alert:Dictionary<String,String> = aps["alert"] as! Dictionary<String,String>

    let title:String = alert["title"]!
    let msg:String   = alert["body"]!
    
  
    let refreshAlert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertController.Style.alert)
    
    refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
         
        refreshAlert.dismiss(animated: true, completion: nil)
      
    }))


    UIApplication.shared.windows.first!.rootViewController?.present(refreshAlert, animated: true, completion: nil)
 
}
Mortenson answered 18/3, 2021 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.