255 lines
7.4 KiB
Python
255 lines
7.4 KiB
Python
ERR_OK = 0
|
|
ERR_PARAM = -1
|
|
ERR_TIMESTAMP = -2
|
|
ERR_SIGN = -3
|
|
ERR_INTERNAL = -4
|
|
ERR_HTTP = -100
|
|
ERR_RETURN_DATA = -101
|
|
|
|
ENV_PROD = 1
|
|
ENV_DEV = 2
|
|
|
|
MESSAGE_TYPE_ANDROID_NOTIFICATION = 1
|
|
MESSAGE_TYPE_ANDROID_MESSAGE = 2
|
|
MESSAGE_TYPE_IOS_APNS_NOTIFICATION = 11
|
|
MESSAGE_TYPE_IOS_REMOTE_NOTIFICATION = 12
|
|
|
|
|
|
#tnps
|
|
class ClickAction(object):
|
|
TYPE_ACTIVITY = 1
|
|
TYPE_URL = 2
|
|
TYPE_INTENT = 3
|
|
|
|
def __init__(self, actionType=1, url='', confirmOnUrl=0, activity='', intent=''):
|
|
self.actionType = actionType
|
|
self.url = url
|
|
self.confirmOnUrl = confirmOnUrl
|
|
self.activity = activity
|
|
self.intent = intent
|
|
self.intentFlag = 0
|
|
self.pendingFlag = 0
|
|
self.packageName = ""
|
|
self.packageDownloadUrl = ""
|
|
self.confirmOnPackage = 1
|
|
|
|
def GetObject(self):
|
|
ret = {}
|
|
ret['action_type'] = self.actionType
|
|
if self.TYPE_ACTIVITY == self.actionType:
|
|
ret['activity'] = self.activity
|
|
ret['aty_attr'] = {'if' :self.intentFlag, 'pf' :self.pendingFlag}
|
|
elif self.TYPE_URL == self.actionType:
|
|
ret['browser'] = {'url' :self.url, 'confirm' :self.confirmOnUrl}
|
|
elif self.TYPE_INTENT == self.actionType:
|
|
ret['intent'] = self.intent
|
|
|
|
return ret
|
|
|
|
class Style(object):
|
|
N_INDEPENDENT = 0
|
|
N_THIS_ONLY = -1
|
|
|
|
def __init__(self, builderId=0, ring=0, vibrate=0, clearable=1, nId=N_INDEPENDENT):
|
|
self.builderId = builderId
|
|
self.ring = ring
|
|
self.vibrate = vibrate
|
|
self.clearable = clearable
|
|
self.nId = nId
|
|
self.ringRaw = ""
|
|
self.lights = 1
|
|
self.iconType = 0
|
|
self.iconRes = ""
|
|
self.styleId = 1
|
|
self.smallIcon = ""
|
|
|
|
class TimeInterval(object):
|
|
STR_START = 'start'
|
|
STR_END = 'end'
|
|
STR_HOUR = 'hour'
|
|
STR_MIN = 'min'
|
|
|
|
def __init__(self, startHour=0, startMin=0, endHour=0, endMin=0):
|
|
self.startHour = startHour
|
|
self.startMin = startMin
|
|
self.endHour = endHour
|
|
self.endMin = endMin
|
|
|
|
def _isValidTime(self, hour, minute):
|
|
return isinstance(hour, int) and isinstance(minute, int) and hour >= 0 and hour <=23 and minute >=0 and minute <= 59
|
|
|
|
def _isValidInterval(self):
|
|
return self.endHour * 60 + self.endMin >= self.startHour * 60 + self.startMin
|
|
|
|
def GetObject(self):
|
|
if not (self._isValidTime(self.startHour, self.startMin) and self._isValidTime(self.endHour, self.endMin)):
|
|
return None
|
|
if not self._isValidInterval():
|
|
return None
|
|
return {
|
|
self.STR_START:{self.STR_HOUR:str(self.startHour), self.STR_MIN:str(self.startMin)},
|
|
self.STR_END:{self.STR_HOUR:str(self.endHour), self.STR_MIN:str(self.endMin)}
|
|
}
|
|
|
|
class Message(object):
|
|
"""
|
|
待推送的消息, Android系统专用
|
|
"""
|
|
PUSH_SINGLE_PKG = 0
|
|
PUSH_ACCESS_ID = 1
|
|
|
|
def __init__(self):
|
|
self.title = ""
|
|
self.content = ""
|
|
self.expireTime = 0
|
|
self.sendTime = ""
|
|
self.acceptTime = ()
|
|
self.type = 0
|
|
self.style = None
|
|
self.action = None
|
|
self.custom = {}
|
|
self.multiPkg = self.PUSH_SINGLE_PKG
|
|
self.raw = None
|
|
self.loopTimes = 0
|
|
self.loopInterval = 0
|
|
|
|
def GetMessageObject(self):
|
|
if self.raw is not None:
|
|
if isinstance(self.raw, basestring):
|
|
return json.loads(self.raw)
|
|
else:
|
|
return self.raw
|
|
|
|
message = {}
|
|
message['title'] = self.title
|
|
message['content'] = self.content
|
|
|
|
# TODO: check custom
|
|
message['custom_content'] = self.custom
|
|
|
|
acceptTimeObj = self.GetAcceptTimeObject()
|
|
if None == acceptTimeObj:
|
|
return None
|
|
elif acceptTimeObj != []:
|
|
message['accept_time'] = acceptTimeObj
|
|
|
|
if self.type == MESSAGE_TYPE_ANDROID_NOTIFICATION:
|
|
if None == self.style:
|
|
style = Style()
|
|
else:
|
|
style = self.style
|
|
|
|
if isinstance(style, Style):
|
|
message['builder_id'] = style.builderId
|
|
message['ring'] = style.ring
|
|
message['vibrate'] = style.vibrate
|
|
message['clearable'] = style.clearable
|
|
message['n_id'] = style.nId
|
|
message['ring_raw'] = style.ringRaw
|
|
message['lights'] = style.lights
|
|
message['icon_type'] = style.iconType
|
|
message['icon_res'] = style.iconRes
|
|
message['style_id'] = style.styleId
|
|
message['small_icon'] = style.smallIcon
|
|
else:
|
|
# style error
|
|
return None
|
|
|
|
if None == self.action:
|
|
action = ClickAction()
|
|
else:
|
|
action = self.action
|
|
|
|
if isinstance(action, ClickAction):
|
|
message['action'] = action.GetObject()
|
|
else:
|
|
# action error
|
|
return None
|
|
elif self.type == MESSAGE_TYPE_ANDROID_MESSAGE:
|
|
pass
|
|
else:
|
|
return None
|
|
|
|
return message
|
|
|
|
def GetAcceptTimeObject(self):
|
|
ret = []
|
|
for ti in self.acceptTime:
|
|
if isinstance(ti, TimeInterval):
|
|
o = ti.GetObject()
|
|
if o is None:
|
|
return None
|
|
else:
|
|
ret.append(ti.GetObject())
|
|
else:
|
|
return None
|
|
return ret
|
|
|
|
class MessageIOS(Message):
|
|
"""
|
|
待推送的消息, iOS系统专用
|
|
"""
|
|
def __init__(self):
|
|
Message.__init__(self)
|
|
self.alert = None
|
|
self.badge = None
|
|
self.sound = None
|
|
self.category = None
|
|
self.raw = None
|
|
self.type = MESSAGE_TYPE_IOS_APNS_NOTIFICATION
|
|
|
|
def GetMessageObject(self):
|
|
if self.raw is not None:
|
|
if isinstance(self.raw, basestring):
|
|
try:
|
|
return json.loads(self.raw)
|
|
except Exception:
|
|
return None
|
|
elif isinstance(self.raw, dict):
|
|
return self.raw
|
|
else:
|
|
return None
|
|
|
|
message = self.custom
|
|
|
|
acceptTimeObj = self.GetAcceptTimeObject()
|
|
if None == acceptTimeObj:
|
|
return None
|
|
elif acceptTimeObj != []:
|
|
message['accept_time'] = acceptTimeObj
|
|
|
|
aps = {}
|
|
if self.type == MESSAGE_TYPE_IOS_APNS_NOTIFICATION:
|
|
if isinstance(self.alert, basestring) or isinstance(self.alert, dict):
|
|
aps['alert'] = self.alert
|
|
else:
|
|
# alert type error
|
|
return None
|
|
if self.badge is not None:
|
|
aps['badge'] = self.badge
|
|
if self.sound is not None:
|
|
aps['sound'] = self.sound
|
|
if self.category is not None:
|
|
aps['category'] = self.category
|
|
elif self.type == MESSAGE_TYPE_IOS_REMOTE_NOTIFICATION:
|
|
aps['content-available'] = 1
|
|
else: # type error
|
|
return None
|
|
message['aps'] = aps
|
|
return message
|
|
|
|
|
|
class MessageStatus(object):
|
|
"""
|
|
推送任务的状态
|
|
"""
|
|
def __init__(self, status, startTime):
|
|
self.status = status
|
|
self.startTime = startTime
|
|
|
|
def __str__(self):
|
|
return str(vars(self))
|
|
|
|
def __repr__(self):
|
|
return self.__str__()
|