You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add WebPush support for Safari
* Update webpush.py based on review
Declare results variable at the start of the block
* Fix typo in warning
Co-authored-by: Cuyler Stuwe <[email protected]>
* Update README.rst
Co-authored-by: James Bligh <[email protected]>
* Fix mailto: space
* Expanded documentation for Web Push (#558)
• Added example code to register WP device
• Fixed issue where call to extract UserAgent didn't include UA
• Added examples on how to send a Web Push message
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Attempt to fix tests
* Update README.rst
---------
Co-authored-by: James Bligh <[email protected]>
Co-authored-by: Cuyler Stuwe <[email protected]>
Co-authored-by: Éloi Rivard <[email protected]>
Co-authored-by: Neil Littlejohns <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Generate client public key (applicationServerKey)
161
-
162
-
.. code-block:: bash
163
-
164
-
vapid --applicationServerKey
165
-
166
-
Application Server Key = BEFuGfKKEFp-kEBMxAIw7ng8HeH_QwnH5_h55ijKD4FRvgdJU1GVlDo8K5U5ak4cMZdQTUJlkA34llWF0xHya70
167
-
168
-
169
-
- Configure settings:
170
-
171
125
- ``WP_PRIVATE_KEY``: Absolute path to your private certificate file: os.path.join(BASE_DIR, "private_key.pem")
172
-
- ``WP_CLAIMS``: Dictionary with the same sub info like claims file: {'sub': "mailto:[email protected]"}
126
+
- ``WP_CLAIMS``: Dictionary with default value for the sub, (subject), sent to the webpush service, This would be used by the service if they needed to reach out to you (the sender). Could be a url or mailto e.g. {'sub': "mailto:[email protected]"}.
173
127
- ``WP_ERROR_TIMEOUT``: The timeout on WebPush POSTs. (Optional)
174
-
- ``WP_POST_URL``: A dictionary (key per browser supported) with the full url that webpush notifications will be POSTed to. (Optional)
175
-
176
-
177
-
- Configure client (javascript):
178
-
179
-
.. code-block:: javascript
180
-
181
-
// Utils functions:
182
-
183
-
functionurlBase64ToUint8Array (base64String) {
184
-
var padding ='='.repeat((4-base64String.length%4) %4)
185
-
var base64 = (base64String + padding)
186
-
.replace(/\-/g, '+')
187
-
.replace(/_/g, '/')
188
-
189
-
var rawData =window.atob(base64)
190
-
var outputArray =newUint8Array(rawData.length)
191
128
192
-
for (var i =0; i <rawData.length; ++i) {
193
-
outputArray[i] =rawData.charCodeAt(i)
194
-
}
195
-
return outputArray;
196
-
}
197
-
198
-
functionloadVersionBrowser () {
199
-
if ("userAgentData"innavigator) {
200
-
// navigator.userAgentData is not available in
201
-
// Firefox and Safari
202
-
constuaData=navigator.userAgentData;
203
-
// Outputs of navigator.userAgentData.brands[n].brand are e.g.
204
-
// Chrome: 'Google Chrome'
205
-
// Edge: 'Microsoft Edge'
206
-
// Opera: 'Opera'
207
-
let browsername;
208
-
let browserversion;
209
-
let chromeVersion =null;
210
-
for (var i =0; i <uaData.brands.length; i++) {
211
-
let brand =uaData.brands[i].brand;
212
-
browserversion =uaData.brands[i].version;
213
-
if (brand.match(/opera|chrome|edge|safari|firefox|msie|trident/i) !==null) {
214
-
// If we have a chrome match, save the match, but try to find another match
215
-
// E.g. Edge can also produce a false Chrome match.
216
-
if (brand.match(/chrome/i) !==null) {
217
-
chromeVersion = browserversion;
218
-
}
219
-
// If this is not a chrome match return immediately
220
-
else {
221
-
browsername =brand.substr(brand.indexOf('')+1);
222
-
return {
223
-
name: browsername,
224
-
version: browserversion
225
-
}
226
-
}
227
-
}
228
-
}
229
-
// No non-Chrome match was found. If we have a chrome match, return it.
230
-
if (chromeVersion !==null) {
231
-
return {
232
-
name:"chrome",
233
-
version: chromeVersion
234
-
}
235
-
}
236
-
}
237
-
// If no userAgentData is not present, or if no match via userAgentData was found,
238
-
// try to extract the browser name and version from userAgent
239
-
constuserAgent=navigator.userAgent;
240
-
var ua = userAgent, tem, M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
The above code makes a call to ``requestPOSTToServer()``, which is not implemented. This is where you should make a call to your Django app to register the Web Push device. Your implementation will vary depending on the design of your web app, here's an example using a Django view and an Ajax call:
358
-
359
-
Example Django view to handle registration:
360
-
361
-
.. code-block:: python
362
-
363
-
from django.http import JsonResponse
364
-
from push_notifications.models import WebPushDevice
Example JavaScript to send registration (requires jQuery):
380
-
381
-
.. code-block:: javascript
382
-
383
-
function requestPOSTToServer ( data ) {
384
-
$.ajax({
385
-
url: '/PATH/DEFINED/IN/URLS.PY/',
386
-
data: {
387
-
'browser': data.browser,
388
-
'p256dh': data.p256dh,
389
-
'auth': data.auth,
390
-
'registration_id': data.registration_id
391
-
},
392
-
dataType: 'json',
393
-
success: function (data) {
394
-
}
395
-
});
396
-
}
129
+
For more information about how to configure WebPush, see `docs/WebPush <https://github.com/jazzband/django-push-notifications/blob/master/docs/WebPush.rst>`_.
0 commit comments