3
votes

I am working on app which receives notifications(using apple push notifications). I am storing these notifications and showing as list in one controller. As far as I understand whenever notification is received didReceiveRemoteNotification is called. When app is in foreground and background I am able to store notification in Db from didReceiveRemoteNotification method. But when app is terminated how can I store notifications?

If user taps on notification when application is terminated I am able to store notification by using lauch options. But if user does not tap the notification when app is terminated how do I store the notification?

1
@nyg My question is about how to store notifications when app is terminated. Link is about handling notifications in background state not terminated stateBharath Reddy
Based on answer of this question stackoverflow.com/questions/35058870/… there seems to be no way to store notifications. But in whatsapp it is working.Bharath Reddy

1 Answers

1
votes

Have you tried using getDeliveredNotifications(completionHandler:)? You can call the following code in e.g. the viewDidLoad method of the controller in which you display the received notifications.

Note that if the user clears the received notifications in the notification center this won't work. You'll have to do as mentioned in the accepted answer of the question you linked in your comment.

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    // notifications: An array of UNNotification objects representing the local
    // and remote notifications of your app that have been delivered and are still
    // visible in Notification Center. If none of your app’s notifications are
    // visible in Notification Center, the array is empty.

    // As said in the documentation, this closure may be executed in a background
    // thread, so if you want to update your UI you'll need to do the following:
    DispatchQueue.main.sync { /* or .async {} */ 
        // update UI
    }
}