Example #1
0
def send_notification(token, aps):
    """
    向目标手机发送通知:
    token - 目标手机的token
    aps - 通知的payload,包括显示数字和内容等
    """
    run_twisted()
    configure({'HOST': 'http://localhost:7077/'})
    # provision('lg2013', open('/usr/share/nginx/www/qianli/apns-dev.pem').read(), 'sandbox')
    # notify('lg2013', token, aps)    
    # provision('com.ashstudio.qianli', open('/usr/share/nginx/www/qianli/development.pem').read(), 'sandbox')
    # provision('com.ashstudio.qianli', open('/usr/share/nginx/www/qianli/apns-dev.pem').read(), 'sandbox')
    # 有且只能provision一次!!!如果有相同的app id, 第二次将视为无效
    provision('com.ashstudio.qianli', open('/usr/share/nginx/www/qianli/apns-pro.pem').read(), 'production')
    provision('com.development.qianli', open('/usr/share/nginx/www/qianli/apns-dev.pem').read(), 'sandbox')
    notify('com.ashstudio.qianli', token, aps)
    notify('com.development.qianli', token, aps)
    # provision('com.ashstudio.qianliAdhocDistribution', open('/usr/share/nginx/www/qianli/apns-pro.pem').read(), 'production')
    # notify('com.ashstudio.qianliAdhocDistribution', token, aps)

    logger = logging.getLogger(__name__)
    logger.debug("send notification LG")
    try:
       feedbacktime =  LastFeedback.objects.get(name = 'APNS')
    except LastFeedback.DoesNotExist:
       feedbacktime = LastFeedback(name = 'APNS', lastfeedback = datetime.datetime.now())
       feedbacktime.save()

    da=datetime.datetime.now()
    daydelta=da.day-feedbacktime.lastfeedback.day
    hourdelta=da.hour-feedbacktime.lastfeedback.hour
    secondsdelta = daydelta * 86400 + hourdelta * 3600
    if secondsdelta>86400:
       logger = logging.getLogger(__name__)
       logger.debug("execute feedback service")
       feedbacktime.lastfeedback = datetime.datetime.now()
       feedbacktime.save()
       feedback('com.ashstudio.qianli', async=True, callback=got_feedback)
Example #2
0
	# Send the most recent unsent message
	msgQuery = "select id,messageText from Message where deviceID=%s and isSent=FALSE ORDER BY createdOn DESC"%(deviceID);
	cursor.execute(msgQuery);
	msgs = cursor.fetchall();
	if not msgs:
		#Only send badge numbers
		notify('tqt', deviceToken, {'aps':{'badge': unRead, 'sound':'default'}});
		print "Send message to %s with no alert and badge %d"%(deviceToken, unRead);
	else:	
		alertMsg = msgs[0][1];
		notify('tqt', deviceToken, {'aps':{'badge':unRead,'alert': alertMsg, 'sound': 'default'}});
		print "Send message to %s"%deviceToken;
		print "Message: "+alertMsg;
		print "unread count: %d"%unRead;
		#Then set the unsent messages as sent
		for msg in msgs:
			msgID = msg[0];
			setQuery = "update Message set isSent=TRUE where id=%s"%msgID;
			cursor.execute(setQuery);
			db.commit();

# Record the inactive clients
inactives = feedback('tqt');
for inactive in inactives:
	time = inactive[0];
	token = inactive[1];
	print "On %s, token %s becomes inactive"%(time, token);
	stmt = "update Device set isActive=FALSE where token='%s'"%token;
	cursor.execute(stmt);
	db.commit();
def initEnv():
	configure({'HOST': 'http://localhost:7077/'})
	provision('iPhoneQQ', open('ck_dev.pem').read(), 'sandbox')
	feedback('iPhoneQQ', async=True, callback=got_feedback)