有没有办法验证iOS注册令牌是有效还是无效?

时间:2015-12-31 19:11:58

标签: ios apple-push-notifications

我已经了解在应用启动时从APNS获取推送注册令牌的正确程序,以便您始终拥有有效令牌。但是对于故障排除,是否有用于检查注册令牌有效性的API,类似于如何验证推送证书?

1 个答案:

答案 0 :(得分:0)

这是使用下面的python脚本的解决方案。 信用解决方案Dara Kong

使用证书

向设备发送测试APNS通知

用法:

push_debug.py [-h] [-s] certificate device_id

位置参数:

certificate    path to push certificate
device_id      device ID

可选参数:

-h, --help     show this help message and exit
-s, --sandbox  Use APNS sandbox environment

通过生产环境发送通知

python push_debug.py my_certificate.pem c35124fd2676d646423705b0721004e3b8426d163e10dbf76b46347a4477f12b

通过沙箱环境发送通知

python push_debug.py -s my_certificate.pem 3f3980225497e1846fb5c2db9e0b3510023402c7a772011106c702d2aec20cc5

将此脚本保存到名为push_debug.py

的文件中
import json
import logging
import os
import socket
import ssl
import struct
import sys
import time
import uuid
import argparse

APNS_HOST = 'gateway.push.apple.com'
APNS_HOST_SANDBOX = 'gateway.sandbox.push.apple.com'
APNS_PORT = 2195

APNS_ERRORS = {
    1:'Processing error',
    2:'Missing device token',
    3:'missing topic',
    4:'missing payload',
    5:'invalid token size',
    6:'invalid topic size',
    7:'invalid payload size',
    8:'invalid token',
    255:'Unknown'
}

def push(cert_path, device, sandbox):
    if not os.path.exists(cert_path):
        logging.error("Invalid certificate path: %s" % cert_path)
        sys.exit(1)

    device = device.decode('hex')
    expiry = time.time() + 3600

    try:
        sock = ssl.wrap_socket(
            socket.socket(socket.AF_INET, socket.SOCK_STREAM),
            certfile=cert_path
        )
        host = APNS_HOST_SANDBOX if sandbox else APNS_HOST
        sock.connect((host, APNS_PORT))
        sock.settimeout(1)
    except Exception as e:
        logging.error("Failed to connect: %s" % e)
        sys.exit(1)

    logging.info("Connected to APNS\n")

    for ident in range(1,4):
        logging.info("Sending %d of 3 push notifications" % ident)

        payload = json.dumps({
            'aps': {
                'alert': 'Push Test %d: %s' % (ident, str(uuid.uuid4())[:8])
            }
        })

        items = [1, ident, expiry, 32, device, len(payload), payload]

        try:
            sent = sock.write(struct.pack('!BIIH32sH%ds'%len(payload), *items))
            if sent:
                logging.info("Message sent\n")
            else:
                logging.error("Unable to send message\n")
        except socket.error as e:
            logging.error("Socket write error: %s", e)

        # If there was an error sending, we will get a response on socket
        try:
            response = sock.read(6)
            command, status, failed_ident = struct.unpack('!BBI',response[:6])
            logging.info("APNS Error: %s\n", APNS_ERRORS.get(status))
            sys.exit(1)
        except socket.timeout:
            pass
        except ssl.SSLError:
            pass

    sock.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Send test APNS notifications to device using cert")
    parser.add_argument("certificate", help="path to push certificate")
    parser.add_argument("device_id", help="Device ID")
    parser.add_argument("-s", "--sandbox", action="store_true", help="Use APNS sandbox environment")
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO)
    push(args.certificate, args.device_id, args.sandbox)
    logging.info("Complete\n")
相关问题