Yupi
Yupi

Reputation: 4470

Local notification data

Hello to all I'm stuck with one issue in my iOS app. I have searched over the internet and didn't find solution. Most of the solutions are using deprecated

func application(_ application: UIApplication, didReceive notification: UILocalNotification)

So scenario is following:

App schedules for each day 5 local notifications and that is okay and that works:

 var dateComponents = DateComponents()
    dateComponents.weekday = Date().dayNumberOfWeek()
    dateComponents.hour = hour
    dateComponents.minute = minute
    dateComponents.second = 5

    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    let request = UNNotificationRequest(identifier: "identifier", content: notifcation, trigger: notificationTrigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

But my question is: is it possible to find out when notification is fired regardless of app state and get data from that notification?

Because time for each notification is retrieved from API I don't know when (hour and minute) notifications will be scheduled.

For example in Android I schedule notifications using AlarmManager API and than easly by extending BroadcastReciver I can fetch every fired notification and get data sent via Intent.

I need to find out when last (fifth) notification is fired for that day so I can preform some additional work.

Upvotes: 0

Views: 578

Answers (1)

LorenzOliveto
LorenzOliveto

Reputation: 7936

In short no, it's not possible to be notified regardless of the app state.

If the app is running in foreground you can do this using UNUserNotificationCenterDelegate methods, but if your app is not in foreground the app will know about the notification only if it's opened from the notification (swiping or tapping it).

Upvotes: 1

Related Questions