Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift Receive Push Notification When App in Foreground

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

like image 813
Kavin Kumar Arumugam Avatar asked Dec 27 '16 07:12

Kavin Kumar Arumugam


2 Answers

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])
}
like image 120
Kavin Kumar Arumugam Avatar answered Sep 28 '22 03:09

Kavin Kumar Arumugam


 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

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

}
like image 28
Sofeda Avatar answered Sep 28 '22 03:09

Sofeda