-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_push.py
More file actions
67 lines (57 loc) · 1.77 KB
/
Copy pathtest_push.py
File metadata and controls
67 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import time
from datetime import datetime, timedelta
from nose.tools import with_setup # type: ignore
import leancloud
from leancloud import push
__author__ = "asaka"
def setup_func():
leancloud.init(os.environ["APP_ID"], os.environ["APP_KEY"])
@with_setup(setup_func)
def test_basic_push(): # type: () -> None
installation = leancloud.Installation()
installation.set("deviceType", "ios")
installation.set("deviceToken", "xxx")
installation.save()
data = {
"alert": {
"title": "标题",
"title-loc-key": "",
"body": "消息内容",
"action-loc-key": "",
"loc-key": "",
"loc-args": [""],
"launch-image": "",
}
}
query = leancloud.Query("_Installation").equal_to("deviceToken", "xxx")
now = datetime.now()
two_hours_later = now + timedelta(hours=2)
try:
notification = push.send(
data,
where=query,
push_time=now,
expiration_time=two_hours_later,
prod="dev",
flow_control=0,
)
except leancloud.LeanCloudError as e:
# LeanCloudError: [1] The iOS certificate file is expired or disabled.
assert e.code == 1
else:
# flow_control = 0 <=> flow_control = 1000 by rest api design
time.sleep(5) # notification write may have delay
notification.fetch()
assert notification.id
# Test that notification is read only.
try:
notification.save()
except leancloud.LeanCloudError as e:
assert e.code == 1
else:
raise Exception()