JJTech0130 / pypush

[being rewritten] Cross-platform iMessage POC
https://discord.gg/BVvNukmfTC
Other
3.52k stars 397 forks source link

Rewrite: APNs: Scoped App Tokens #101

Closed JJTech0130 closed 1 month ago

JJTech0130 commented 1 month ago

Allows receiving notifications sent from the public APNs API

JJTech0130 commented 1 month ago

Note to self: the current receive API relies on the fact that the network is slower to respond than the scheduler is to schedule the receive task, as there is no backlog. Not necessarily an incorrect assumption, but should be noted.

JJTech0130 commented 1 month ago

The API is finished, it involved developing a new "Stream Filters" mechanism internally, but it leads to a nice clean test like this:

@pytest.mark.asyncio
async def test_scoped_token():
    async with apns.create_apns_connection(
        *await apns.activate(), courier="1-courier.sandbox.push.apple.com"
    ) as connection:

        token = await connection.mint_scoped_token("dev.jjtech.pypush.tests")

        test_message = f"test-message-{uuid.uuid4().hex}"

        # Not part of pypush, uses Apple's public APNs API
        await send_test_notification(token.hex(), test_message.encode())

        await connection.expect_notification(
            "dev.jjtech.pypush.tests",
            token,
            lambda c: c if c.payload == test_message.encode() else None,
        )
JJTech0130 commented 1 month ago

New notifications subcommand, for example: pypush notifications dev.jjtech.pypush.tests --sandbox

Screenshot 2024-05-18 at 8 39 46 PM

Note that it currently does not provide any method of saving the certificate or tokens used.

You can test it out using your own CloudKit console if you have a Developer ID, or use the .pem file in the tests/assets/ directory and use it with any of the online APNs test websites.