Skip to content

Commit 3e24d5e

Browse files
committed
mobpush api for python init
0 parents  commit 3e24d5e

11 files changed

+484
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
*.class
2+
# Mobile Tools for Java (J2ME)
3+
.mtj.tmp/
4+
# Package Files #
5+
*.war
6+
*.ear
7+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
8+
hs_err_pid*
9+
*.settings
10+
*.prefs
11+
org.eclipse.core.resources.prefs
12+
target
13+
*.classpath
14+
*.project
15+
.externalToolBuilders/
16+
logs/
17+
*.log
18+
.DS_Store
19+
20+
#idea
21+
*.idea
22+
*.iml
23+
.svn
24+
*.svn
25+
26+
#project
27+
.pydevproject

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [MobPush API for Python](http://wiki.mob.com/mobpush-rest-api-接口文档/)
2+
3+
![image](https://github.com/MOBX/MOB-SMS-WEBAPI/blob/master/doc/images/logo.png)
4+
5+
**[MobPush API for Python](http://wiki.mob.com/mobpush-rest-api-接口文档/)**
6+
为了帮助开发者更方便接入MobPush免费推送SDK,提供完整的API接口的python实现,包含设备操作相关接口、推送操作相关接口以及公共接口。
7+
8+
了解更多 [MobPush 免费推送SDK.](http://mobpush.mob.com)
9+
10+
11+
## 优势
12+
13+
**免费使用****自定义UI****稳定服务****流程体验****数据同步****专业技术团队服务**
14+
15+
## 接口
16+
* 推送接口
17+
* 发送推送
18+
* 查询推送(根据batchId)
19+
* 查询推送(根据workno)
20+
* 推送统计接口
21+
* 查询推送统计(根据batchId)
22+
* 查询推送统计(根据workno)
23+
* 别名操作接口
24+
* 查询别名
25+
* 设置别名
26+
* 标签操作接口
27+
* 查询标签
28+
* 设置标签
29+
* 公共接口
30+
* 地理位置信息接口
31+
32+
33+
## 使用注意事项
34+
* 初始化appkey、appSecret , 可以在 mob目录下tools.py 设置
35+
* 错误码请参考
36+
[MobPush Api 错误码](http://wiki.mob.com/mobpush-rest-api-接口文档/#map-6)
37+
38+
## 使用DEMO
39+
40+
发送推送示例片段代码
41+
42+
```xml
43+
# 初始化推送
44+
panel = push().initPush(appkey='moba6b6c6d6', content = 'content test', plats = [1,2])
45+
# 设置推送范围
46+
target = push().buildTarget(target = 1, tags = None, alias = None, registrationIds = None, city = None, block = None)
47+
# 设置Android定制信息
48+
android = push().buildAndroid(androidTitle = '安卓标题')
49+
# 设置iOS 定制信息
50+
ios= push().bulidIos(iosTitle = 'ios 标题')
51+
# 设置推送扩展信息
52+
extra = push().buildExtra(unlineTime = 1)
53+
54+
js = tools().json_join(panel, target)
55+
js = tools().json_join(js,android)
56+
js = tools().json_join(js,ios)
57+
js = tools().json_join(js,extra)
58+
res = push().sendPush(js)
59+
print(res)
60+
61+
62+
```

mob/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
'''
5+
@Created: 2018年2月7日
6+
@author: hlliu
7+
@site: http://wiki.mob.com/mobpush-rest-api-接口文档/#map-0
8+
@version: 1.0.0
9+
'''
141 Bytes
Binary file not shown.

mob/__pycache__/device.cpython-36.pyc

875 Bytes
Binary file not shown.

mob/__pycache__/tools.cpython-36.pyc

2.3 KB
Binary file not shown.

mob/area.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
'''
5+
@Created: 2018年2月7日
6+
@author: hlliu
7+
@site: http://wiki.mob.com/mobpush-rest-api-接口文档/#map-0
8+
@version: 1.0.0
9+
@note: 获取地理位置列表信息
10+
'''
11+
12+
from tools import tools
13+
import json
14+
15+
class area():
16+
17+
baseUrl = "http://api.push.mob.com"
18+
19+
# 获取地理位置列表 -- 子级列表
20+
def getArea(self, parentId = '0'):
21+
path = '%s%s%s' % (self.baseUrl , "/area/" , parentId)
22+
result=tools().web_get(path,'')
23+
return result
24+

mob/device.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
'''
5+
@Created: 2018年2月7日
6+
@author: hlliu
7+
@site: http://wiki.mob.com/mobpush-rest-api-接口文档/#map-0
8+
@version: 1.0.0
9+
@note: 设备相关接口实现: 别名查询、别名设置、别名清除、标签查询、标签新增、标签删除、标签清除
10+
'''
11+
12+
from tools import tools
13+
import json
14+
15+
class device:
16+
17+
deviceUrl = 'http://api.push.mob.com'
18+
19+
# 获取设备别名,返回别名
20+
def getDeviceAlias(self, registrationId):
21+
if registrationId.strip() == '':
22+
return {'status':-1,'error':'registrationId is null'}
23+
path= '%s%s%s' % (self.deviceUrl,'/alias/',registrationId)
24+
req=tools().web_get(path,'')
25+
alias = req.get("alias")
26+
return alias
27+
28+
# 设置设备别名
29+
def setDeviceAlias(self,registrationId,alias):
30+
if registrationId.strip() == '':
31+
return {'status':-1,'error':'registrationId is null'}
32+
path= '%s%s' % (self.deviceUrl,'/alias')
33+
jsondata = {'alias':alias,'registrationId':registrationId}
34+
req=tools().web_post(path, jsondata)
35+
error = req.get("error")
36+
if error is None or error.strip() == '':
37+
return json.dumps({'status':200});
38+
else :
39+
return req;
40+
41+
# 清除设备别名
42+
def cleanDeviceAlias(self,registrationId):
43+
return self.setDeviceAlias(registrationId,'')
44+
45+
# 获取设备标签
46+
def getDeviceTags(self,registrationId):
47+
if registrationId.strip() == '':
48+
return {'status':-1,'error':'registrationId is null'}
49+
path= '%s%s%s' % (self.deviceUrl,'/tags/',registrationId)
50+
req=tools().web_get(path,'')
51+
tags = req.get("tags")
52+
return tags
53+
54+
# 绑定设置设备标签
55+
def addDeviceTags(self, tags, registrationId):
56+
if tags is None:
57+
return {'status':-1,'error':'tags is null'}
58+
if registrationId.strip() == '':
59+
return {'status':-1,'error':'registrationId is null'}
60+
path= '%s%s' % (self.deviceUrl,'/tags')
61+
jsondata = {'tags':tags,'registrationId':registrationId,"opType":1}
62+
req=tools().web_post(path, jsondata)
63+
error = req.get("error")
64+
if error is None or error.strip() == '':
65+
return json.dumps({'status':200});
66+
else :
67+
return req;
68+
69+
# 解绑设备标签
70+
def removeDeviceTags(self, tags, registrationId):
71+
if tags is None:
72+
return {'status':-1,'error':'tags is null'}
73+
if registrationId.strip() == '':
74+
return {'status':-1,'error':'registrationId is null'}
75+
path= '%s%s' % (self.deviceUrl,'/tags')
76+
jsondata = {'tags':tags,'registrationId':registrationId,"opType":2}
77+
req=tools().web_post(path, jsondata)
78+
error = req.get("error")
79+
if error is None or error.strip() == '':
80+
return json.dumps({'status':200});
81+
else :
82+
return req;
83+
84+
# 清除设备标签
85+
def cleanDeviceTags(self, registrationId):
86+
if registrationId.strip() == '':
87+
return {'status':-1,'error':'registrationId is null'}
88+
path= '%s%s' % (self.deviceUrl,'/tags')
89+
jsondata = {'registrationId':registrationId,"opType":3}
90+
req=tools().web_post(path, jsondata)
91+
error = req.get("error")
92+
if error is None or error.strip() == '':
93+
return json.dumps({'status':200});
94+
else :
95+
return req;
96+

mob/push.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
'''
5+
@Created: 2018年2月7日
6+
@author: hlliu
7+
@site: http://wiki.mob.com/mobpush-rest-api-接口文档/#map-0
8+
@version: 1.0.0
9+
@note: 发送推送、查询推送详情
10+
'''
11+
12+
from tools import tools
13+
import json
14+
15+
class push():
16+
17+
pushUrl = "http://api.push.mob.com"
18+
19+
# 推送详情(根据workno查询)
20+
def getPushByWorkno(self, workno):
21+
if workno.strip() == '':
22+
return {'status':-1,'error':'workno is null'}
23+
path = '%s%s%s' % (self.pushUrl , "/push/workno/" , workno)
24+
result=tools().web_get(path,'')
25+
return result
26+
27+
# 推送详情(根据batchId查询)
28+
def getPushByBatchId(self, batchId):
29+
if batchId.strip() == '':
30+
return {'status':-1,'error':'batchId is null'}
31+
path = '%s%s%s' % (self.pushUrl , "/push/id/" , batchId)
32+
result=tools().web_get(path,'')
33+
return result
34+
35+
36+
# 发送推送
37+
def sendPush(self,pushwork):
38+
print('')
39+
if pushwork is None:
40+
return {'status':-1,'error':'pushwork is null'}
41+
appkey = pushwork['appkey']
42+
if appkey is None or appkey.strip() == '':
43+
return {'status':-1,'error':'appkey is null'}
44+
target = pushwork['target']
45+
if target is None:
46+
return {'status':-1,'error':'target is null'}
47+
type = pushwork['type']
48+
if type is None:
49+
return {'status':-1,'error':'type is null'}
50+
content = pushwork['content']
51+
if content is None:
52+
return {'status':-1,'error':'content is null'}
53+
path = '%s%s' % (self.pushUrl , "/push")
54+
result=tools().web_post(path,pushwork)
55+
return result
56+
57+
58+
# push消息初始化
59+
# type = 1 通知, type = 2 自定义
60+
# plats 数组格式,包含 android 和 iOS 为 [1,2]
61+
def initPush(self, appkey, workno = None, plats = [1,2], content = None, push_type = 1):
62+
panel = {}
63+
if appkey is not None:
64+
panel['appkey'] = appkey
65+
if workno is not None:
66+
panel['workno'] = workno
67+
if plats is not None:
68+
panel['plats'] = plats
69+
if content is not None:
70+
panel['content'] = content
71+
if push_type is not None:
72+
panel['type'] = push_type
73+
return panel
74+
75+
# 设置扩展信息
76+
def buildExtra(self, unlineTime = 1, extras = None, iosProduction = 1):
77+
panel = {}
78+
if extras is not None:
79+
panel['extras'] = extras
80+
return panel
81+
82+
# 设置推送范围
83+
# target:推送范围:1广播;2别名;3标签;4regid;5地理位置;6用户分群
84+
# tags 、alias 、 registrationIds是数组方式
85+
def buildTarget(self, target = 1, tags = None, alias = None, registrationIds = None, city = None, block = None):
86+
panel = {}
87+
if target is None:
88+
return panel
89+
panel['target'] = target
90+
if target == 1:
91+
return panel
92+
elif target == 2:
93+
if alias is not None:
94+
panel['alias'] = alias
95+
elif target == 3:
96+
if tags is not None:
97+
panel['tags'] = tags
98+
elif target == 4:
99+
if registrationIds is not None:
100+
panel['registrationIds'] = registrationIds
101+
elif target == 5:
102+
if city.strip() == '':
103+
panel['city'] = city
104+
elif target == 6:
105+
if block.strip() == '':
106+
panel['block'] = block
107+
return panel
108+
109+
# 设置Android信息
110+
# androidTitle : 0(普通通知),1(大段文字内容),2(大图模式),3(横幅通知)
111+
# androidContent 样式具体内容: 0、默认通知无; 1、长内容则为内容数据; 2、大图则为图片地址; 3、横幅则为多行内容
112+
def buildAndroid(self, androidTitle = None, androidstyle = None, androidContent = None,
113+
androidVoice = None, androidShake = None, androidLight = None):
114+
panel = {}
115+
if androidTitle is not None:
116+
panel['androidTitle'] = androidTitle
117+
if androidstyle is not None:
118+
panel['androidstyle'] = androidstyle
119+
if androidContent is not None:
120+
panel['androidContent'] = androidContent
121+
if androidVoice is not None:
122+
panel['androidVoice'] = androidVoice
123+
if androidShake is not None:
124+
panel['androidShake'] = androidShake
125+
if androidLight is not None:
126+
panel['androidLight'] = androidLight
127+
return panel
128+
129+
# 设置IOS信息
130+
def bulidIos(self, iosTitle = None, iosSubtitle = None, iosSound = 'default', iosBadge = 1, iosCategory = None,
131+
iosSlientPush = None, iosContentAvailable = None, iosMutableContent = None):
132+
panel = {}
133+
if iosTitle is not None:
134+
panel['iosTitle'] = iosTitle
135+
if iosSubtitle is not None:
136+
panel['iosSubtitle'] = iosSubtitle
137+
if iosSound is not None:
138+
panel['iosSound'] = iosSound
139+
if iosBadge is not None:
140+
panel['iosBadge'] = iosBadge
141+
if iosCategory is not None:
142+
panel['iosCategory'] = iosCategory
143+
if iosSlientPush is not None:
144+
panel['iosSlientPush'] = iosSlientPush
145+
if iosContentAvailable is not None:
146+
panel['iosContentAvailable'] = iosContentAvailable
147+
if iosMutableContent is not None:
148+
panel['iosMutableContent'] = iosMutableContent
149+
return panel
150+
151+
152+

0 commit comments

Comments
 (0)