def test_platform_priority_apns(self):
     self.assertEqual(
         PlatformPriority.platform_priority(PlatformType.APNS,
                                            PlatformPriority.HIGH), '10')
     self.assertEqual(
         PlatformPriority.platform_priority(PlatformType.APNS,
                                            PlatformPriority.NORMAL), '5')
 def test_platform_priority_webpush(self):
     self.assertEqual(
         PlatformPriority.platform_priority(PlatformType.WEBPUSH,
                                            PlatformPriority.HIGH), 'high')
     self.assertEqual(
         PlatformPriority.platform_priority(PlatformType.WEBPUSH,
                                            PlatformPriority.NORMAL),
         'normal')
 def test_platform_priority_android(self):
     self.assertEqual(
         PlatformPriority.platform_priority(PlatformType.ANDROID,
                                            PlatformPriority.HIGH), 'high')
     self.assertEqual(
         PlatformPriority.platform_priority(PlatformType.ANDROID,
                                            PlatformPriority.NORMAL),
         'normal')
    def __init__(self, collapse_key=None, priority=None):
        """
        Args:
            collapse_key (string): Collapse key for push notification - may be None.
            priority (int): Priority for push notification - may be None.
        """
        self.collapse_key = collapse_key

        # Check that our priority looks right
        if priority:
            PlatformPriority.validate(priority)
        self.priority = priority
    def platform_config(self, platform_type):
        """ Return a platform-specific configuration object for a platform_type, given the platform payload.

        Args:
            platform_type (PlatformType): Type for the platform config.

        Returns:
            object: Either a AndroidConfig, ApnsConfig, or WebpushConfig depending on the platform_type.
        """
        from consts.fcm.platform_type import PlatformType
        # Validate that platform_type is supported
        PlatformType.validate(platform_type)

        from firebase_admin import messaging
        if platform_type == PlatformType.ANDROID:
            priority = PlatformPriority.platform_priority(platform_type, self.priority) \
                if self.priority is not None else None

            return messaging.AndroidConfig(
                collapse_key=self.collapse_key,
                priority=priority
            )
        else:
            headers = {}

            if self.collapse_key:
                headers[PlatformType.collapse_key_key(platform_type)] = self.collapse_key

            if self.priority is not None:
                priority = PlatformPriority.platform_priority(platform_type, self.priority)
                headers[PlatformType.priority_key(platform_type)] = priority

            # Null out headers if they're empty
            headers = headers if headers else None

            if platform_type == PlatformType.APNS:
                # Create an empty `payload` as a workaround for an FCM bug
                # https://github.com/the-blue-alliance/the-blue-alliance/pull/2557#discussion_r310365295
                payload = messaging.APNSPayload(aps=messaging.Aps(content_available=True)) if headers else None
                return messaging.APNSConfig(headers=headers, payload=payload)
            elif platform_type == PlatformType.WEBPUSH:
                return messaging.WebpushConfig(headers=headers)
            else:
                raise TypeError("Unsupported PlatformPayload platform_type: {}".format(platform_type))
 def test_validate_invalid(self):
     with self.assertRaises(ValueError):
         PlatformPriority.validate(2)
 def test_platform_priority_invalid_priority(self):
     with self.assertRaises(ValueError):
         PlatformPriority.platform_priority(PlatformType.ANDROID, -1)
 def test_platform_priority_invalid_platform(self):
     with self.assertRaises(ValueError):
         PlatformPriority.platform_priority(-1, PlatformPriority.HIGH)
 def test_validate(self):
     PlatformPriority.validate(PlatformPriority.NORMAL)
     PlatformPriority.validate(PlatformPriority.HIGH)