@@ -283,21 +283,76 @@ def __init__(self, headers=None, data=None, notification=None):
283
283
self .notification = notification
284
284
285
285
286
+ class WebpushNotificationAction (object ):
287
+ """An action available to the users when the notification is presented.
288
+
289
+ Args:
290
+ action: Action string.
291
+ title: Title string.
292
+ icon: Icon URL for the action (optional).
293
+ """
294
+
295
+ def __init__ (self , action , title , icon = None ):
296
+ self .action = action
297
+ self .title = title
298
+ self .icon = icon
299
+
300
+
286
301
class WebpushNotification (object ):
287
302
"""Webpush-specific notification parameters.
288
303
304
+ Refer to the `Notification Reference`_ for more information.
305
+
289
306
Args:
290
307
title: Title of the notification (optional). If specified, overrides the title set via
291
308
``messaging.Notification``.
292
309
body: Body of the notification (optional). If specified, overrides the body set via
293
310
``messaging.Notification``.
294
311
icon: Icon URL of the notification (optional).
312
+ actions: A list of ``messaging.WebpushNotificationAction`` instances (optional).
313
+ badge: URL of the image used to represent the notification when there is
314
+ not enough space to display the notification itself (optional).
315
+ data: Any arbitrary JSON data that should be associated with the notification (optional).
316
+ direction: The direction in which to display the notification (optional). Must be either
317
+ 'auto', 'ltr' or 'rtl'.
318
+ image: The URL of an image to be displayed in the notification (optional).
319
+ language: Notification language (optional).
320
+ renotify: A boolean indicating whether the user should be notified after a new
321
+ notification replaces an old one (optional).
322
+ require_interaction: A boolean indicating whether a notification should remain active
323
+ until the user clicks or dismisses it, rather than closing automatically (optional).
324
+ silent: True to indicate that the notification should be silent (optional).
325
+ tag: An identifying tag on the notification (optional).
326
+ timestamp_millis: A timestamp value in milliseconds on the notification (optional).
327
+ vibrate: A vibration pattern for the device's vibration hardware to emit when the
328
+ notification fires (optional). THe pattern is specified as an integer array.
329
+ custom_data: A dict of custom key-value pairs to be included in the notification
330
+ (optional)
331
+
332
+ .. _Notification Reference: https://developer.mozilla.org/en-US/docs/Web/API\
333
+ /notification/Notification
295
334
"""
296
335
297
- def __init__ (self , title = None , body = None , icon = None ):
336
+ def __init__ (self , title = None , body = None , icon = None , actions = None , badge = None , data = None ,
337
+ direction = None , image = None , language = None , renotify = None ,
338
+ require_interaction = None , silent = None , tag = None , timestamp_millis = None ,
339
+ vibrate = None , custom_data = None ):
298
340
self .title = title
299
341
self .body = body
300
342
self .icon = icon
343
+ self .actions = actions
344
+ self .badge = badge
345
+ self .data = data
346
+ self .direction = direction
347
+ self .image = image
348
+ self .language = language
349
+ self .renotify = renotify
350
+ self .require_interaction = require_interaction
351
+ self .silent = silent
352
+ self .tag = tag
353
+ self .timestamp_millis = timestamp_millis
354
+ self .vibrate = vibrate
355
+ self .custom_data = custom_data
301
356
302
357
303
358
class APNSConfig (object ):
@@ -579,15 +634,68 @@ def encode_webpush_notification(cls, notification):
579
634
raise ValueError ('WebpushConfig.notification must be an instance of '
580
635
'WebpushNotification class.' )
581
636
result = {
637
+ 'actions' : cls .encode_webpush_notification_actions (notification .actions ),
638
+ 'badge' : _Validators .check_string (
639
+ 'WebpushNotification.badge' , notification .badge ),
582
640
'body' : _Validators .check_string (
583
641
'WebpushNotification.body' , notification .body ),
642
+ 'data' : notification .data ,
643
+ 'dir' : _Validators .check_string (
644
+ 'WebpushNotification.direction' , notification .direction ),
584
645
'icon' : _Validators .check_string (
585
646
'WebpushNotification.icon' , notification .icon ),
647
+ 'image' : _Validators .check_string (
648
+ 'WebpushNotification.image' , notification .image ),
649
+ 'lang' : _Validators .check_string (
650
+ 'WebpushNotification.language' , notification .language ),
651
+ 'renotify' : notification .renotify ,
652
+ 'requireInteraction' : notification .require_interaction ,
653
+ 'silent' : notification .silent ,
654
+ 'tag' : _Validators .check_string (
655
+ 'WebpushNotification.tag' , notification .tag ),
656
+ 'timestamp' : _Validators .check_number (
657
+ 'WebpushNotification.timestamp_millis' , notification .timestamp_millis ),
586
658
'title' : _Validators .check_string (
587
659
'WebpushNotification.title' , notification .title ),
660
+ 'vibrate' : notification .vibrate ,
588
661
}
662
+ direction = result .get ('dir' )
663
+ if direction and direction not in ('auto' , 'ltr' , 'rtl' ):
664
+ raise ValueError ('WebpushNotification.direction must be "auto", "ltr" or "rtl".' )
665
+ if notification .custom_data is not None :
666
+ if not isinstance (notification .custom_data , dict ):
667
+ raise ValueError ('WebpushNotification.custom_data must be a dict.' )
668
+ for key , value in notification .custom_data .items ():
669
+ if key in result :
670
+ raise ValueError (
671
+ 'Multiple specifications for {0} in WebpushNotification.' .format (key ))
672
+ result [key ] = value
589
673
return cls .remove_null_values (result )
590
674
675
+ @classmethod
676
+ def encode_webpush_notification_actions (cls , actions ):
677
+ """Encodes a list of WebpushNotificationActions into JSON."""
678
+ if actions is None :
679
+ return None
680
+ if not isinstance (actions , list ):
681
+ raise ValueError ('WebpushConfig.notification.actions must be a list of '
682
+ 'WebpushNotificationAction instances.' )
683
+ results = []
684
+ for action in actions :
685
+ if not isinstance (action , WebpushNotificationAction ):
686
+ raise ValueError ('WebpushConfig.notification.actions must be a list of '
687
+ 'WebpushNotificationAction instances.' )
688
+ result = {
689
+ 'action' : _Validators .check_string (
690
+ 'WebpushNotificationAction.action' , action .action ),
691
+ 'title' : _Validators .check_string (
692
+ 'WebpushNotificationAction.title' , action .title ),
693
+ 'icon' : _Validators .check_string (
694
+ 'WebpushNotificationAction.icon' , action .icon ),
695
+ }
696
+ results .append (cls .remove_null_values (result ))
697
+ return results
698
+
591
699
@classmethod
592
700
def encode_apns (cls , apns ):
593
701
"""Encodes an APNSConfig instance into JSON."""
0 commit comments