1

Motive:- I want to store payload of notification stored when notification received by app while it is not in background mode or it is killed mode.

issues:- No delegate calls while app getting notification in killed mode.Please suggest what to do in this situations.

sagarSaw
  • 89
  • 11

2 Answers2

1

From the Apple documentation (UNUserNotificaitonCenter framework iOS 10+)...

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("didReceive \(response.notification.request.content.userInfo)")
}

The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    print("willPresent: \(notification.request.content.userInfo)")
    completionHandler([.alert, .badge, .sound])
}

The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
1

You can't do that when app is killed or quit. But you can retrieve the delivered notifications and process them when app is opened again. You can get the notifications using following process.

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    for aNoitfication in notifications
    {
        let payload = aNoitfication.request.content.userInfo
        //process the payload
    }
    DispatchQueue.main.sync { /* or .async {} */ 
        // update UI
    }
}

P.S: Which is available only on iOS 10 or later

Ravi
  • 2,441
  • 1
  • 11
  • 30