Пример #1
0
Файл: v3.py Проект: pjongy/jraze
    async def send_data(
        self,
        targets: List[str],
        data: dict
    ) -> Tuple[int, int]:
        collapse_key = str(uuid4())
        results = await asyncio.gather(*[
            self.apns.send_notification(
                NotificationRequest(
                    device_token=target,
                    message=data,
                    collapse_key=collapse_key,
                    push_type=PushType.ALERT,
                )
            )
            for target in targets
        ])

        success = 0
        failed = len(targets)
        for response in results:
            if response.is_successful:
                success += 1
        failed -= success

        return success, failed
async def send_push(token, title, subtitle, priority, additionalInfo):
    apns_key_client = APNs(
        key="My_APNS_Auth_Key.p8",
        key_id="FromApplePortal<p8 key id>",
        team_id="FromApplePortal<team id>",
        topic="com.your.bundleId",
        use_sandbox=True,
    )
    request = NotificationRequest(
        device_token=token,
        message={
            "aps": {
                "sound": "",
                "content-available": 1,
                "alert": {
                    "title": title,
                    "body": subtitle
                },
            },
            "anyAdditionalData": additionalInfo,
        },
        priority=priority,
        push_type=PushType.BACKGROUND if priority == "5" else PushType.ALERT,
    )

    await apns_key_client.send_notification(request)
    print("Fired notification on device {} with priority {}".format(
        token, priority))
Пример #3
0
    async def _dispatch_request(self, log, span, device, shaved_payload, prio):
        """
        Actually attempts to dispatch the notification once.
        """

        # this is no good: APNs expects ID to be in their format
        # so we can't just derive a
        # notif_id = context.request_id + f"-{n.devices.index(device)}"

        notif_id = str(uuid4())

        log.info(f"Sending as APNs-ID {notif_id}")
        span.set_tag("apns_id", notif_id)

        device_token = base64.b64decode(device.pushkey).hex()

        request = NotificationRequest(
            device_token=device_token,
            message=shaved_payload,
            priority=prio,
            notification_id=notif_id,
        )

        try:

            with ACTIVE_REQUESTS_GAUGE.track_inprogress():
                with SEND_TIME_HISTOGRAM.time():
                    response = await self._send_notification(request)
        except aioapns.ConnectionError:
            raise TemporaryNotificationDispatchException(
                "aioapns Connection Failure")

        code = int(response.status)

        span.set_tag(tags.HTTP_STATUS_CODE, code)

        RESPONSE_STATUS_CODES_COUNTER.labels(pushkin=self.name,
                                             code=code).inc()

        if response.is_successful:
            return []
        else:
            # .description corresponds to the 'reason' response field
            span.set_tag("apns_reason", response.description)
            if (code == self.TOKEN_ERROR_CODE
                    or response.description == self.TOKEN_ERROR_REASON):
                return [device.pushkey]
            else:
                if 500 <= code < 600:
                    raise TemporaryNotificationDispatchException(
                        f"{response.status} {response.description}")
                else:
                    raise NotificationDispatchException(
                        f"{response.status} {response.description}")
Пример #4
0
async def send_push_notification(device_token, badge, alert={}):
    apns_key_client = APNs(
        key=os.path.join(settings.BASE_DIR, 'logged_in_assets',
                         'AuthKey_X44S38MRBS.p8'),
        key_id=settings.IOS_KEY_ID,  # Key ID
        team_id=settings.IOS_TEAM_ID,  # Team ID
        topic=settings.IOS_APP_BUNDLE_ID,  # Bundle ID
        use_sandbox=True,
    )
    request = NotificationRequest(
        device_token=device_token,
        message={
            "aps": {
                "alert": {
                    "title": alert["title"],
                    "body": alert["body"],
                    "selectedIndex": alert["selected_index"]
                },
                "badge": badge,
            }
        },
    )
    await apns_key_client.send_notification(request)
Пример #5
0
async def run():
    apns_cert_client = APNs(
        client_cert='/path/to/apns-cert.pem',  # 令牌
        use_sandbox=False,
    )
    apns_key_client = APNs(
        key='/path/to/apns-key.p8',  # 私钥
        key_id='<KEY_ID>',  # 10个字符的密钥标识符(kid)密钥
        team_id='<TEAM_ID>',  # 您的10个字符的团队ID
        topic=
        'com.okmusician.ios',  # 远程通知的主题,通常是应用程序的软件包ID。您在开发人员帐户中创建的证书必须包含此主题的功能。
        use_sandbox=False,
    )
    request = NotificationRequest(
        device_token='<DEVICE_TOKEN>',  # user token
        message={"aps": {
            "alert": "Hello from APNs",
            "badge": "1",
        }},
        notification_id=str(uuid4()),  # optional
        time_to_live=3,  # optional
    )
    await apns_cert_client.send_notification(request)
    await apns_key_client.send_notification(request)
Пример #6
0
 async def send_request():
     request = NotificationRequest(device_token=device_token,
                                   message=message)
     await apns.send_notification(request)