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

Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47

5 Answers5

8

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])
}
Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47
1
 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

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

}
Sofeda
  • 1,361
  • 1
  • 13
  • 23
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

Sagar Rathode
  • 243
  • 1
  • 15
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
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
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)
 
}
Blazej Kita
  • 99
  • 1
  • 1
  • 10