Apple Push Notifications arriving on MacOS, but not in the application itself
Asked Answered
C

0

16

I'm running an iOS one-time password (OTP) application, and a corresponding MacOS companion app. In the iOS application, users are able to tap on one-time passwords, resulting in the iOS application sending an HTTP request to my server. My server in turn, will send a push notification to the MacOS application, containing the tapped one-time password. All encrypted of course, but that's irrelevant for the issue.

iOS application -> HTTP request -> My server -> APS -> MacOS application

Recently, after users started upgrading to MacOS Ventura, the MacOS application stopped receiving notifications. I can see (and I've verified) that the process from the iOS application to the server, to APS works. APS returns a 200 OK success status, indicating that the push notification was sent to the MacOS device.

However, in my MacOS application the notification is not received in the didReceiveRemoteNotification function (which I included below).

/// Sent to the delegate when a running application receives a remote notification.
///
/// - Parameter application: The application that received the remote notification.
/// - Parameter userInfo: A dictionary that contains information related to the remote notification, specifically a badge number for the application icon, a notification identifier, and possibly custom data.
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
    NotificationHelper.shared.notify(userInfo)
    
    log.verbose("Received a remote notification in the application delegate.")
}

The didRegisterForRemoteNotificationsWithDeviceToken function does work, and I can see it succeeded because of my applications' debug logging.

/// Sent to the delegate when Apple Push Services successfully completes the registration process.
///
/// - Parameter application: The application that initiated the remote-notification registration process.
/// - Parameter deviceToken: A token that identifies the device to Apple Push Notification Service (APNS).
func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    settingsView.pushToken.data = deviceToken
    linkingView.pushToken.data = deviceToken
    
    log.verbose("Successfully completed APNS registration process.")
}

I've verified that I'm using an actual production key for APS, and that the MacOS application itself is a production build as well (or well, TestFlight in my case). I used the same production key and build before users started upgrading to Ventura, and it always worked.

What's interesting is that, although I can't see anything in Xcode or in my debug logging of the production build of the app, the Console.app of MacOS does show the push notification being received. I've included some of the Console.app APS logging (from when a notification should be received in my MacOS application) below.

default 20:35:11.555463+0100    apsd    <private>: Outstanding data received: <private> (length 490)
default 20:35:11.555624+0100    apsd    <private>: Stream processing: complete yes, invalid no, length parsed 490, parameters <private>
default 20:35:11.555761+0100    apsd    <private>: Failed to find token name for unrecognized token <private>
default 20:35:11.555876+0100    apsd    <private>: Message has token <private> (tokenName (null))
error   20:35:11.556008+0100    apsd    <private>: Received message for unknown token '<private>'
error   20:35:11.556068+0100    apsd    <private>: Ignoring duplicate message received with topic: '<private>' timestamp=1671564911431255965 (57.5 seconds ago)
default 20:35:11.556186+0100    apsd    APSPowerLog: {event: <private>, dict: <private>}
default 20:35:11.556432+0100    apsd    <private>: Sending acknowledgement message with response 7 and messageId <private> (3889788835)
default 20:35:11.556591+0100    apsd    <private>: Sending Push ACK. UUID: (null)
default 20:35:11.556735+0100    apsd    <private>: Stream processing: complete no, invalid no, length parsed 0, parameters (null)

The following two lines caught my eye, and those of some other users of my app debugging the issue.

error   20:35:11.556008+0100    apsd    <private>: Received message for unknown token '<private>'
error   20:35:11.556068+0100    apsd    <private>: Ignoring duplicate message received with topic: '<private>' timestamp=1671564911431255965 (57.5 seconds ago)

However, searching for these error message on the internet does not yield any (helpful) results. Therefore, I would like to ask you on StackOverflow for help!

Any ideas on why I'm not receiving these notifications correctly in my MacOS app are welcome. Hopefully we can get to a solution.


As my apps are open-source, it might be useful to take a look at the code. I've added links to the Github projects below.

For users of my app where push notifications are working, the following logging is visible in Console.app.

default 16:00:48.398808+0100    apsd    <private>: Message has token <private> (tokenName <private>)
default 16:00:48.398969+0100    apsd    <private>: Received message for enabled topic '<private>' onInterface: NonCellular with payload '<private>' with priority 5
default 16:00:48.399208+0100    apsd    <private>: Push has been read from stream and parsed. UUID: (null)
default 16:00:48.399323+0100    apsd    APSPowerLog: {event: <private>, dict: <private>}
default 16:00:48.399481+0100    apsd    <private> asked to store incoming message <private> with guid <private> environment <private>
default 16:00:48.401221+0100    apsd    APSMessageStore - New message record [<private>] has ID [16196].
default 16:00:48.401381+0100    apsd    <private>: Sending acknowledgement message with response 0 and messageId <private> (37785720)
default 16:00:48.401473+0100    apsd    <private>: Sending Push ACK. UUID: (null)
Commodious answered 20/12, 2022 at 20:25 Comment(8)
Irrespective of the code and app, the messaging tokens that you are generating, have you tried to send notifications using plain curls ? If I were you I would see 1. what token is generated ? 2. For that token, using the server key, I shall hit APNS apis for sending notifications directly using token and server key. And first make sure that its working fine. Its possible that something has been changed in payload ? or pirority or token generation mechanism on iOS side.Abattoir
Token can get updated on reset or OS upgrade. I am not seeing anywhere in your code where you update the token you get on Mac on your server. Do you generate a new QR code when token gets updated? Do users scan the new code or use the code with an outdated token?Randalrandall
@Randalrandall QR-code is generated on registering for APS in the MacOS application. Although tokens may change from time to time, in reality they rarely do. I do have confirmed, as well as the users of my app debugging the issue, that I'm sending the notification to an active token, which I can see is active on my MacOS device as the token is being registered in my MacOS app.Commodious
Maybe it's not a problem in the code? It could also be the case here with Ventura, as with iOS, that notifications are received by the OS but not displayed due to settings. The first thing that comes to mind is <System settings/Notifications>. Also, have all users already updated to 13.1?Thesaurus
We are having the same problem. Same log entries. Someone suggested this, but it didn't help: sudo killall -9 apsdAdham
To be honest, it seems to me like a bug in MacOS. I've filed a bug report at Apple but they didn't respond yet.Commodious
The service has a device -> APNs relationship, so I think you might want to try another macOS system? You can't just try a new user or app... Apple isn't explicit, but the apsd should be keeping the deviceToken list locally , the usual risks of cache corruption (and rarelyamplified by an upgrade event), could be creating problems on your test Mac.Dashtilut
I have the same issue, on debug working fine but installing from TestFlight, not works. Did you find solution for it?Refreshment

© 2022 - 2024 — McMap. All rights reserved.