Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default_payload in Push Payloads #127

Merged
merged 24 commits into from
Jun 24, 2020
Merged

Use default_payload in Push Payloads #127

merged 24 commits into from
Jun 24, 2020

Conversation

ismailgulek
Copy link
Contributor

Add fallback_content case handling from push data to include alert and mutable-content fields in the aps dictionary.

Part of element-hq/element-ios#3325

@ismailgulek ismailgulek requested a review from a team June 10, 2020 12:49
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ismailgulek, I've got a few questions I'm afraid...

@@ -0,0 +1 @@
Add fallback_content handling from push data.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you try to expand on this a bit? What is fallback_content? what does handling it involve? does this affect both iOS and Android, or just one of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, fallback_content is needed because we need to add alert in the aps field of payload. The handling is this: if we see this value as true in the data, we're adding alert to the payload. It's because of this:

The system executes your notification content app extension only when a remote notification’s payload contains the following information:

  • The payload must include the mutable-content key with a value of 1.
  • The payload must include an alert dictionary with title, subtitle, or body information.

Detailed info can be found at https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications

It only affects iOS and does not affect Android or other pusher types.

sygnal/apnspushkin.py Outdated Show resolved Hide resolved
@@ -299,6 +299,13 @@ def _get_payload_event_id_only(self, n):
if n.counts.missed_calls is not None:
payload["missed_calls"] = n.counts.missed_calls

payload["aps"] = {"mutable-content": 1}
if (
n.devices[0].data["fallback_content"] is not None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we only look at the first device? Shouldn't this be looking at the device we are preparing the payload for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Updated.

payload["aps"] = {"mutable-content": 1}
if (
n.devices[0].data["fallback_content"] is not None
and n.devices[0].data["fallback_content"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to be specced?

Copy link
Contributor Author

@ismailgulek ismailgulek Jun 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that, the fallback_content will be an additional field in data property of https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-pushers-set. Plus homeserver just passes this data according to this:

A dictionary of additional pusher-specific data. For 'http' pushers, this is the data dictionary passed in at pusher creation minus the url key.

Detailed info can be found here: https://matrix.org/docs/spec/push_gateway/r0.1.1#post-matrix-push-v1-notify

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, for client implementations to know what to set, and for pushgateway implementations to know how to interpret it, it has to be specced, otherwise this becomes secret knowledge which will only work with riot-ios and sygnal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as per matrix-org/matrix-spec-proposals#2631 (comment): no, it doesn't need to be in the spec. Sorry!

That said, I do think it would be useful if we documented somewhere within the sygnal project what parameters the apnspushkin supports in its data dict and what they do. Perhaps you could add these details under https://github.com/matrix-org/sygnal#app-types ?

@ismailgulek ismailgulek requested a review from richvdh June 11, 2020 07:00
sygnal/apnspushkin.py Outdated Show resolved Hide resolved
sygnal/apnspushkin.py Show resolved Hide resolved
payload["aps"] = {"mutable-content": 1}
if (
n.devices[0].data["fallback_content"] is not None
and n.devices[0].data["fallback_content"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, for client implementations to know what to set, and for pushgateway implementations to know how to interpret it, it has to be specced, otherwise this becomes secret knowledge which will only work with riot-ios and sygnal.

@ismailgulek ismailgulek changed the title Add Fallback Content Case Handling Use default_payload in Push Payloads Jun 18, 2020
@ismailgulek
Copy link
Contributor Author

Decided to update this with a more general solution.

Offering use of default_payload in data which will be some JSON data, set by the clients. This will be even more helpful because in this way clients will have more freedom to use mutable/silent or any other type of notifications, and also have possibility to select alert string or dictionary in the way they want. These are some configurations for iOS side, but also Android side can have some default payload.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things to fix here.

Please can you add some documentation for each of the two pushkins, which documents the structure of the data field they expect?

sygnal/apnspushkin.py Outdated Show resolved Hide resolved
sygnal/apnspushkin.py Outdated Show resolved Hide resolved
sygnal/apnspushkin.py Outdated Show resolved Hide resolved
sygnal/apnspushkin.py Outdated Show resolved Hide resolved
sygnal/apnspushkin.py Show resolved Hide resolved
sygnal/gcmpushkin.py Outdated Show resolved Hide resolved
ismailgulek and others added 8 commits June 22, 2020 18:07
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
@ismailgulek
Copy link
Contributor Author

A couple of things to fix here.

Please can you add some documentation for each of the two pushkins, which documents the structure of the data field they expect?

Sure, is it ok to add these in Pushkin classes, like:

Relays notifications to the Apple Push Notification Service.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good now. I look forward to seeing the docs!

@ismailgulek
Copy link
Contributor Author

code looks good now. I look forward to seeing the docs!

thanks, README updated

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of suggestions

README.rst Outdated Show resolved Hide resolved
README.rst Outdated
Comment on lines 71 to 77
When calling https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-pushers-set, clients
can provide a ``default_payload`` data dictionary in ``PusherData``, which will be passed to Sygnal.
This will be used by Sygnal to have a default data dictionary in the push payload, for both APNS and
GCM pushers, if provided. Upon the default payload dictionary, Sygnal will make incremental changes.
This is useful for clients to decide default push payload content. For instance, iOS clients will have
freedom to use silent/mutable notifications and be able to set some default alert/sound/badge fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be clearer to start a list here.

The following parameters can be specified in the `data` dictionary which is given when configuring the pusher
via `POST /_matrix/client/r0/pushers/set <https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-pushers-set>`_ :

* ``default_payload``: a dictionary which defines the basic payload to be sent to the notification service. 
  Sygnal will merge information specific to the push event into this dictionary. If unset, the empty dictionary is used.

  This can be useful for clients to specify default push payload content.... etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, updated

ismailgulek and others added 2 commits June 23, 2020 14:10
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
README.rst Outdated
=======

The following parameters can be specified in the `data` dictionary which is given when configuring the pusher
via [POST /_matrix/client/r0/pushers/set](<https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-pushers-set>) :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is RST, so this format doesn't work. I think I got it right in my suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry yes, you're right. I was bitten by my previewer. Updated.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, thank you for bearing with it!

@richvdh richvdh merged commit 34f60f6 into master Jun 24, 2020
@richvdh richvdh deleted the riot_3325 branch June 24, 2020 09:00
@ismailgulek
Copy link
Contributor Author

looks great, thank you for bearing with it!

Great, thanks for your valuable comments.

@myoussef89
Copy link

myoussef89 commented Jun 28, 2020

I got this error when I send it empty, what shoud be sent inside ?:

Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: 2020-06-28 22:08:54,462 [4208] ERROR sygnal.http [40efe488-af22-41ee-8fc2-da59be7238b7] Exception whilst dispatching notification.
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: Traceback (most recent call last):
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: File "/home/ubuntu/sygnal/sygnal/http.py", line 265, in _handle_dispatch
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: result = await pushkin.dispatch_notification(notif, d, context)
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: File "/home/ubuntu/sygnal/sygnal/gcmpushkin.py", line 326, in dispatch_notification
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: data = GcmPushkin._build_data(n, device)
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: File "/home/ubuntu/sygnal/sygnal/gcmpushkin.py", line 410, in _build_data
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: data.update(device.data.get("default_payload", {}))
Jun 28 22:08:54 ip-172-31-11-241 python3[4208]: ValueError: dictionary update sequence element #0 has length 1; 2 is required

"data": {
"default_payload": {},
"format": "event_id_only",
"url": "https://********************/_matrix/push/v1/notify"
}

@ismailgulek
Copy link
Contributor Author

I got this error when I send it empty, what shoud be sent inside ?:

Nothing actually. I've tested your data dictionary and it was fine in my Sygnal. I'm not sure what's the root cause but you may remove default_payload field.

@myoussef89
Copy link

it's working without it, but I don't receive any notification on ios side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants