简体   繁体   中英

Silent push notification not working on foreground iOS app

I am having trouble getting my app to receive silent push notifications while it is in the foreground. I have correctly set up push notifications and can receive "alert" notifications (ones that have an alert, sound, or badge) no problem no matter what state the app is in. But when I send the following push notification payload:

{ 
    "aps": { 
        "content-available": 1
    }
}

My app will not receive it via the app delegate application:didReceiveRemoteNotification:completionHandler: . I have "Remote notification" checked and enabled in Signing & Capabilities.

The crazy thing is, I know I am sending it correctly and the phone is receiving it correctly because if I change my run scheme to "Wait for executable to be launched" and run the app, sending that very same notification will launch the app automatically (and the push notification payload will be included in launchOptions ). I just can't see any reason why my delegate method wouldn't be called if the app is already in the foreground (or even the background).

Has anyone experienced this before and possibly know where to look to see if I've altered a setting incorrectly somewhere? This is on an iPhone running 13.3 and using Xcode 11.3.

Well, after struggling with this for days I stumbled upon my answer barely an hour after posting this question.

To get background notifications delivered to your app you must call UIApplication.shared.registerForRemoteNotifications() every time your app is launched. I was skipping the call if I already had the token and this was causing my delegate method to not be called.

apns-push-type : Required for watchOS 6 and later; recommended for macOS, iOS, tvOS, and iPadOS) The value of this header must accurately reflect the contents of your notification's payload. If there is a mismatch, or if the header is missing on required systems, APNs may return an error, delay the delivery of the notification, or drop it altogether. Use the background push type for notifications that deliver content in the background, and don't trigger any user interactions. https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM