2
votes

After receiving the remote notification and the user takes action (other than close/dismiss), the app delegate gets callbacks:

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

or if the app is registered for notification actions:

-(void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler

My question is does the app get a callback when the remote notification is received? That is before the user takes any action. Thanks for your input.

4
@SausageMachine clarified my question...Loozie
Why do you need to know that the notification has reached the device before being sent to the app?rmaddy
@rmaddy we're in the middle of designing a system and the backend guy asked me if it's possible to get a delegate call back when notification is received... i just want to double check the SO community. thanks for your responseLoozie
@SausageMachine I beg the difference. I love to talk to fellow developers. Thanks for chiming in.Loozie
@Loozie. Its possible to send an app-directed push (called silent or background push) to the app. The app can receive that and then post a local notification if the user needs to see something. Hence in effect this is the same as what you were asking (as the user gets to see the notification and the app knows the notification is there because it put it here). However, if the app has been forcefully terminated by the user, the background push will not get delivered to the app.Gruntcakes

4 Answers

1
votes

No, the app doesn't get any indication that the notification has reached the device. It only gets a delegate call when the notification actually gets sent to the app as you describe in the two cases in your question.

1
votes
extension AppDelegate: UNUserNotificationCenterDelegate {

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert,.sound,.badge])
        print("NOtification received")
    }

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("original body was : \(response.notification.request.content.title)")
    print("Tapped in notification")

    }
}
0
votes

It could be possible to make use of background update notifications, as described in https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app However the use-cases for this solution are limited by the low number of notifications that are allowed to be sent in this way (the docs currently state "don't try to send more than 2 or 3 per hour").

-1
votes

Yes, Of course app gets callback of these below delegates method when remote notification is received.

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("DEVICE TOKEN = \(deviceToken)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print(error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    print(userInfo)
}