Files
rjb_push_server/test_honor_push.py

106 lines
3.5 KiB
Python
Raw Permalink Normal View History

2026-01-04 16:51:47 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试 Honor 推送脚本
"""
import json
import requests
import time
# 推送服务地址
PUSH_SERVER_URL = "http://localhost:8080/android/push"
# 用户提供的 token
DEVICE_TOKEN = "BAEAAAAAB.josybFY8YYNOK7suCSammWuFIaIUgCdo1d5Ud2NBTUWnyy2a8yUG2WpwNiTZFgBW3sRPO_q-a1bWjwu_ODI6HWHHszoUi1HbhlhMaxjHmOs-zxfg--SECc"
# Honor 推送类型(注意:实际运行的推送服务版本中使用的是 8不是 9
ANDROID_PUSH_TYPE_HONOR = 8
# 普通消息类型
PUSH_MESSAGE_TYPE_NORMAL = 0
def send_honor_push(title="测试消息", content="这是一条测试推送消息", sender="test_user", target="honor_device"):
"""
发送 Honor 推送消息
Args:
title: 推送标题
content: 推送内容
sender: 发送者ID
target: 接收者ID
"""
# 构建推送消息
push_message = {
"sender": sender,
"senderName": "测试用户",
"senderPortrait": "",
"convType": 0, # 0: 单聊, 1: 群聊, 3: 聊天室
"target": target,
"targetName": "Honor设备",
"targetPortrait": "",
"userId": target,
"line": 0,
"cntType": 1, # 消息内容类型
"serverTime": int(time.time() * 1000), # 服务器时间(毫秒)
"pushMessageType": PUSH_MESSAGE_TYPE_NORMAL, # 0: 普通消息
"pushType": ANDROID_PUSH_TYPE_HONOR, # 9: Honor推送
"pushContent": content, # 推送内容
"pushData": "", # 推送数据(可选)
"unReceivedMsg": 1, # 未接收消息数
"mentionedType": 0, # 0: 无@提醒, 1: @了当前用户, 2: @了所有人
"packageName": "com.xunpaisoft.social.im", # 应用包名根据配置文件中的badgeClass推断
"deviceToken": DEVICE_TOKEN, # 设备token
"voipDeviceToken": "",
"isHiddenDetail": False, # 是否隐藏详情
"language": "zh_CN", # 语言
"messageId": 0,
"callStartUid": 0,
"republish": False,
"existBadgeNumber": 0
}
print("=" * 60)
print("发送 Honor 推送消息")
print("=" * 60)
print(f"推送服务地址: {PUSH_SERVER_URL}")
print(f"设备Token: {DEVICE_TOKEN[:50]}...")
print(f"推送标题: {title}")
print(f"推送内容: {content}")
print("=" * 60)
try:
# 发送 POST 请求
response = requests.post(
PUSH_SERVER_URL,
json=push_message,
headers={"Content-Type": "application/json;charset=UTF-8"},
timeout=10
)
print(f"\n响应状态码: {response.status_code}")
print(f"响应内容: {response.text}")
if response.status_code == 200:
print("\n✓ 推送请求发送成功!")
print("请检查手机是否收到推送通知。")
else:
print(f"\n✗ 推送请求失败,状态码: {response.status_code}")
except requests.exceptions.ConnectionError:
print("\n✗ 无法连接到推送服务!")
print("请确保推送服务正在运行在 http://localhost:8080")
except requests.exceptions.Timeout:
print("\n✗ 请求超时!")
except Exception as e:
print(f"\n✗ 发生错误: {str(e)}")
if __name__ == "__main__":
# 发送测试推送
send_honor_push(
title="测试推送",
content="这是一条来自推送服务的测试消息,如果您收到此消息,说明推送配置成功!",
sender="test_sender_001",
target="honor_device_001"
)