5
votes

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

5
Do you want to access user info from Notification object?PlusInfosys
@iOS_devloper yes sirKavin Kumar Arumugam
I am using "notification.request.content.userInfo" to get data. You can print this in above method.Amanpreet
You can use notification.request.content.userInfoPlusInfosys
@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!AL.

5 Answers

8
votes

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])
}
1
votes
 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

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

}
1
votes

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

1
votes

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
0
votes
  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)
 
}