0
votes

I have implemented push notification for ios10. Tapping on the notification alert would trigger "didReceive" delegate, were i save the notification in coredata or silent notification if i m in foreground. The problem is if i receive a stack of notification in background and When i bring my app to foreground from background, Is there a possibility to call "didReceive" delegate or any other push notification delegate were i could sync my items to coredata.

Note I don't want to sync(didReceive or any delegate) the items in background using silent notification nor tapping on the alert. It should sync automatically the stack of push notification when i bring the app to foreground

     func handleInboxNotification(didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    if let inboxValue = userInfo["inbox"] as? String, let value = inboxValue.boolValue(), value {
        let mappedMessage = MessageCenterSDK.shared.constructReorderedDictionary(userInfo: userInfo)
        MessageCenterDataManager.shared.synchronize(messages: mappedMessage)
        messageCenterDelegate?.didFindNewMessages(hasNewMessages: true)
    }
}


func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    /// Handle outbound remote
    handleInboxNotification(didReceiveRemoteNotification: response.notification.request.content.userInfo)
    completionHandler()
}
2

2 Answers

2
votes

I was facing the same issue, but my target was for iOS 13 & I'm using BGTaskScheduler to fetch data from the server in the background as well in terminated state.

I too want to trigger the notification when the app is in background. But not tapped but that seems to be not possible so we have changed our implementation by enabling background mode and it worked for me.

Hope it also helps.

For more reference on BGTaskScheduler, https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler

-1
votes

you need to use willpresent delegate in appDelegate

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Change this to your preferred presentation option
    completionHandler([])
}

This delegate is called when a notification comes in foreground