コード例 #1
0
def payload_alert():
    return PayloadAlert(title='title',
                        title_localized_key='loc_k',
                        title_localized_args=['loc_a'],
                        body='body',
                        body_localized_key='body_loc_k',
                        body_localized_args=['body_loc_a'],
                        action_localized_key='ac_loc_k',
                        action='send',
                        launch_image='img')
コード例 #2
0
	def create_payload(self, background=None, title="Title", body="Body", silent=True, badge=0): # pylint: disable=R0913
		"""
		Constructs APNS payload
		"""
		if silent:
			sound = None
		else:
			sound = "Default"
		if background is not None:
			data = {"Data": background}
			self.payload = Payload(content_available=True, custom=data)
		else:
			alert = PayloadAlert(title=title, body=body)
			self.payload = Payload(alert=alert, sound=sound, badge=badge)
コード例 #3
0
ファイル: bark.py プロジェクト: qq1074123922/py-bark
def push(category, title, body, device_token, params):
    payload = Payload(alert=PayloadAlert(title=None if title == '' else title,
                                         body=body),
                      sound='1107',
                      badge=int(params['badge']) if 'badge' in params else 0,
                      category=category,
                      mutable_content=True,
                      custom=params)
    client = APNsClient(CERT_PATH)

    try:
        client.send_notification(device_token, payload, 'me.fin.bark')
        return ''
    except APNsException as e:
        return str(e)
コード例 #4
0
    def send_push(self, title, body):
        connects = UserConnectionField.objects.find_type_connection_by_user(
            self.user, UserConnectionField.APNS_TYPE)

        client = APNsClient(settings.APNS_KEY_LOCATION,
                            use_sandbox=settings.APNS_SENDBOX,
                            use_alternative_port=False)

        payload = Payload(alert=PayloadAlert(title=title, body=body),
                          sound="default",
                          badge=1)
        for connect in connects:
            client.send_notification(token_hex=connect.value,
                                     notification=payload,
                                     topic=settings.APNS_BUNDLE_ID)
コード例 #5
0
def sendMultipleNotifications(tokens, title, body, flightString, dateString):
    costumDict = {
        "flightString": flightString,
        "dateString": dateString,
    }
    payload = Payload(alert=PayloadAlert(title=title, body=body),
                      custom=costumDict,
                      sound="default",
                      badge=0)
    topic = 'com.MihaStrah.FlightTracker'
    token_credentials = TokenCredentials(
        auth_key_path="AppleAuthentication/AuthKey_85KZTANBJ8.p8",
        auth_key_id="85KZTANBJ8",
        team_id="7YNLV7443U")
    client = APNsClient(credentials=token_credentials, use_sandbox=False)
    Notification = collections.namedtuple('Notification', ['token', 'payload'])
    notifications = []

    for token in tokens:
        logger.info("notification sent to APNS")
        notifications.append(Notification(payload=payload, token=token))

    #pošljemo skupek obvestil (za vse uporabnike, ki so naročeni na let)
    client.send_notification_batch(notifications=notifications, topic=topic)
コード例 #6
0
 def setUp(self):
     self.payload_alert = payload_alert = PayloadAlert(
         title='title', title_localized_key='loc_k', title_localized_args='loc_a',
         body='body', body_localized_key='body_loc_k', body_localized_args='body_loc_a',
         action_localized_key='ac_loc_k', action='send',
         launch_image='img')
コード例 #7
0
def send_group_message(obj):
    appid = obj["appid"]
    sender = obj["sender"]
    receivers = obj["receivers"]
    group_id = obj["group_id"]

    appname = get_title(appid)
    sender_name = user.get_user_name(rds, appid, sender)
    group_name = get_group_name(group_id)
    title = group_name if group_name else appname

    content = push_content(sender_name, obj["content"])

    try:
        c = json.loads(obj["content"])
        collapse_id = c.get('uuid')
        if "revoke" in c:
            collapse_id = c['revoke']['msgid']
            sender_name = sender_name if sender_name else ''
            content = "%s撤回了一条消息" % sender_name
        at = c.get('at', [])
        at_all = c.get('at_all', False)
    except ValueError:
        at = []
        at_all = False
        collapse_id = None

    extra = {}
    extra["sender"] = sender
    extra["group_id"] = group_id

    apns_users = []
    jp_users = []
    xg_users = []
    hw_users = []
    gcm_users = []
    mi_users = []
    ali_users = []

    # 群聊中被at的用户
    at_users = []
    for receiver in receivers:
        do_not_disturb = Conversation.get_do_not_disturb(rds,
                                                         appid,
                                                         receiver,
                                                         group_id=group_id)
        if do_not_disturb:
            logging.info("uid:%d group id:%d do not disturb", receiver,
                         group_id)

        if do_not_disturb and receiver not in at:
            continue

        u = user.get_user(rds, appid, receiver)
        if u is None:
            logging.info("uid:%d nonexist", receiver)
            continue

        if (at_all or receiver in at) and sender_name:
            at_users.append(u)
            continue

        # 找出最近绑定的token
        ts = max(u.apns_timestamp, u.xg_timestamp, u.mi_timestamp,
                 u.hw_timestamp, u.gcm_timestamp, u.ali_timestamp,
                 u.jp_timestamp)

        if u.apns_device_token and u.apns_timestamp == ts:
            apns_users.append(u)
        elif u.xg_device_token and u.xg_timestamp == ts:
            xg_users.append(u)
        elif u.mi_device_token and u.mi_timestamp == ts:
            mi_users.append(u)
        elif u.hw_device_token and u.hw_timestamp == ts:
            hw_users.append(u)
        elif u.gcm_device_token and u.gcm_timestamp == ts:
            gcm_users.append(u)
        elif u.ali_device_token and u.ali_timestamp == ts:
            ali_users.append(u)
        elif u.jp_device_token and u.jp_timestamp == ts:
            jp_users.append(u)
        else:
            logging.info("uid:%d has't device token", receiver)

    for u in at_users:
        content = "%s在群聊中@了你" % sender_name
        push_message_u(appid, appname, u, content, extra)

    for u in xg_users:
        xg_push(appid, appname, u.xg_device_token, content, extra)

    for u in hw_users:
        HuaWeiPush.push(appid, title, u.hw_device_token, content)

    gcm_device_tokens = [u.gcm_device_token for u in gcm_users]
    if gcm_device_tokens:
        FCMPush.push_batch(appid, title, gcm_device_tokens, content)

    for u in ali_users:
        AliPush.push(appid, appname, u.ali_device_token, content)

    # ios apns
    notifications = []
    for u in apns_users:
        sound = 'default'
        alert = PayloadAlert(title=group_name, body=content)
        payload = Payload(alert=alert,
                          sound=sound,
                          badge=u.unread + 1,
                          custom=extra)
        token = u.apns_device_token
        n = Notification(token, payload, None)
        notifications.append(n)

    if notifications:
        IOSPush.push_group_batch(appid, notifications, collapse_id)

    for u in apns_users:
        user.set_user_unread(rds, appid, receiver, u.unread + 1)

    # 极光推送
    #tokens = []
    #for u in jp_users:
    #    tokens.append(u.jp_device_token)
    #if tokens:
    #    JGPush.push(appid, appname, tokens, content)

    tokens = []
    for u in mi_users:
        tokens.append(u.mi_device_token)

    if tokens:
        MiPush.push_batch(appid, title, tokens, content)