I need to get integer delivered by API request when the app gets push notification using One signal to update the badge icon's number. My AppDelegate is like this. I want to send API request every time the app gets push notifications, but is it possible?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
OneSignal.initWithLaunchOptions(launchOptions)
OneSignal.setAppId(Constants.OneSignalAppId)
OneSignal.promptForPushNotifications(userResponse: { accepted in
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
print("User accepted notifications: \(accepted)")
})
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let request = APIRequest()
Session.send(request) { result in
switch result {
case .success(let data):
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = data.totalUnreadMessageDetailCount
}
case .failure(let error):
print(error)
}
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let request = APIRequest()
Session.send(request) { result in
switch result {
case .success(let data):
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = data.totalUnreadMessageDetailCount
}
completionHandler(.badge)
case .failure(let error):
print(error)
}
}
}