#!/bin/bash

# OPPO推送测试脚本 (使用8080端口)
# 使用方法: ./test_push_8080.sh <device_token>
# device_token: OPPO手机的registration_id，需要从OPPO手机应用中获取

if [ -z "$1" ]; then
    echo "=========================================="
    echo "OPPO推送测试脚本 (端口: 8080)"
    echo "=========================================="
    echo ""
    echo "使用方法: $0 <device_token>"
    echo ""
    echo "device_token: OPPO手机的registration_id"
    echo "              (需要从OPPO手机应用中获取)"
    echo ""
    echo "示例: $0 1234567890abcdef1234567890abcdef"
    echo ""
    echo "如果没有device_token，请："
    echo "1. 在OPPO手机上安装应用 (包名: com.xunpaisoft.social)"
    echo "2. 启动应用并查看日志获取registration_id"
    echo "3. 或者从OPPO推送平台查看设备列表"
    exit 1
fi

DEVICE_TOKEN=$1
SERVER_URL="http://localhost:8080/android/push"

echo "=========================================="
echo "正在测试OPPO推送..."
echo "=========================================="
echo "设备Token: $DEVICE_TOKEN"
echo "服务器地址: $SERVER_URL"
echo "包名: com.xunpaisoft.social"
echo ""

# 构建推送消息JSON
# pushType=5 表示 OPPO 推送类型
TIMESTAMP=$(date +%s)
MESSAGE_ID=$TIMESTAMP

RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "$SERVER_URL" \
  -H "Content-Type: application/json;charset=UTF-8" \
  -d "{
    \"pushType\": 5,
    \"pushMessageType\": 0,
    \"packageName\": \"com.xunpaisoft.social\",
    \"deviceToken\": \"$DEVICE_TOKEN\",
    \"pushContent\": \"这是一条测试推送消息 - $(date '+%Y-%m-%d %H:%M:%S')\",
    \"sender\": \"test_user\",
    \"senderName\": \"测试用户\",
    \"target\": \"test_target\",
    \"targetName\": \"测试目标\",
    \"convType\": 0,
    \"line\": 0,
    \"cntType\": 1,
    \"serverTime\": ${TIMESTAMP}000,
    \"unReceivedMsg\": 1,
    \"mentionedType\": 0,
    \"isHiddenDetail\": false,
    \"language\": \"zh\",
    \"messageId\": $MESSAGE_ID,
    \"republish\": false,
    \"existBadgeNumber\": 0
  }")

HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_CODE:/d')

echo "----------------------------------------"
echo "服务器响应:"
echo "----------------------------------------"
echo "$BODY" | python -m json.tool 2>/dev/null || echo "$BODY"
echo ""
echo "HTTP状态码: $HTTP_CODE"
echo ""

if [ "$HTTP_CODE" = "200" ]; then
    echo "✓ 推送请求已发送成功！"
    echo ""
    echo "请检查："
    echo "1. OPPO手机是否收到推送通知"
    echo "2. 查看服务器日志了解详细推送结果"
else
    echo "✗ 推送请求失败 (HTTP $HTTP_CODE)"
    echo ""
    echo "请检查："
    echo "1. 服务器是否正常运行在8080端口"
    echo "2. device_token 是否正确"
    echo "3. 查看服务器日志: tail -f push.log 或查看控制台输出"
fi

echo ""
echo "=========================================="

