#!/bin/bash

# OPPO推送 - 使用新通道方式测试（通过category标记）
# 根据OPPO平台说明：如果标记category使用新通道能力，用户的通知二级开关默认开启

if [ -z "$1" ]; then
    echo "使用方法: $0 <device_token>"
    exit 1
fi

DEVICE_TOKEN=$1

# OPPO推送配置
APP_KEY="bb0819c889ae40cd8bde5a8ad4e670fe"
APP_SERVER_SECRET="2d8b4e922d60453d987f0d09de6eb4a6"
API_URL="https://api.push.oppomobile.com"

echo "=========================================="
echo "OPPO推送 - 新通道方式测试（使用category标记）"
echo "=========================================="

# 1. 获取 auth_token
TIMESTAMP=$(date +%s)000
SIGN_STRING="${APP_KEY}${TIMESTAMP}${APP_SERVER_SECRET}"
SIGN=$(echo -n "$SIGN_STRING" | sha256sum | awk '{print $1}')

AUTH_RESPONSE=$(curl -s -X POST "${API_URL}/server/v1/auth" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "app_key=${APP_KEY}&timestamp=${TIMESTAMP}&sign=${SIGN}")

AUTH_TOKEN=$(echo "$AUTH_RESPONSE" | grep -o '"auth_token":"[^"]*' | cut -d'"' -f4)

if [ -z "$AUTH_TOKEN" ]; then
    echo "错误: 无法获取 auth_token"
    exit 1
fi

echo "✓ 获取 auth_token 成功"
echo ""

# 2. 尝试方式1: 使用category标记，不指定channel_id（让系统自动使用新通道）
echo "方式1: 使用category=IM，不指定channel_id..."
MESSAGE_JSON1=$(cat <<EOF
{
  "target_type": 1,
  "target_value": "$DEVICE_TOKEN",
  "notification": {
    "title": "知你",
    "content": "我是api - 新通道测试1",
    "category": "IM",
    "notify_level": 2,
    "offLine": true,
    "offLineTtl": 86400,
    "showTimeType": 0,
    "networkType": 0
  }
}
EOF
)

PUSH_RESPONSE1=$(curl -s -X POST "${API_URL}/server/v1/message/notification/unicast" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "message=$(echo "$MESSAGE_JSON1" | python -c "import sys, json, urllib.parse; print(urllib.parse.quote(json.dumps(json.load(sys.stdin))))" 2>/dev/null || echo "$MESSAGE_JSON1")&auth_token=${AUTH_TOKEN}")

echo "响应1:"
echo "$PUSH_RESPONSE1" | python -m json.tool 2>/dev/null || echo "$PUSH_RESPONSE1"
echo ""

# 3. 尝试方式2: 使用已存在的通道ID
echo "方式2: 使用已存在的通道ID (previte_message)..."
MESSAGE_JSON2=$(cat <<EOF
{
  "target_type": 1,
  "target_value": "$DEVICE_TOKEN",
  "notification": {
    "title": "知你",
    "content": "我是api - 新通道测试2",
    "channel_id": "previte_message",
    "channel_name": "消息推送",
    "category": "IM",
    "notify_level": 2,
    "offLine": true,
    "offLineTtl": 86400,
    "showTimeType": 0,
    "networkType": 0
  }
}
EOF
)

PUSH_RESPONSE2=$(curl -s -X POST "${API_URL}/server/v1/message/notification/unicast" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "message=$(echo "$MESSAGE_JSON2" | python -c "import sys, json, urllib.parse; print(urllib.parse.quote(json.dumps(json.load(sys.stdin))))" 2>/dev/null || echo "$MESSAGE_JSON2")&auth_token=${AUTH_TOKEN}")

echo "响应2:"
echo "$PUSH_RESPONSE2" | python -m json.tool 2>/dev/null || echo "$PUSH_RESPONSE2"
echo ""

# 4. 尝试方式3: 使用其他已存在的通道ID
echo "方式3: 使用其他通道ID (oppo_push_im)..."
MESSAGE_JSON3=$(cat <<EOF
{
  "target_type": 1,
  "target_value": "$DEVICE_TOKEN",
  "notification": {
    "title": "知你",
    "content": "我是api - 新通道测试3",
    "channel_id": "oppo_push_im",
    "channel_name": "重要消息通知",
    "category": "IM",
    "notify_level": 2,
    "offLine": true,
    "offLineTtl": 86400,
    "showTimeType": 0,
    "networkType": 0
  }
}
EOF
)

PUSH_RESPONSE3=$(curl -s -X POST "${API_URL}/server/v1/message/notification/unicast" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "message=$(echo "$MESSAGE_JSON3" | python -c "import sys, json, urllib.parse; print(urllib.parse.quote(json.dumps(json.load(sys.stdin))))" 2>/dev/null || echo "$MESSAGE_JSON3")&auth_token=${AUTH_TOKEN}")

echo "响应3:"
echo "$PUSH_RESPONSE3" | python -m json.tool 2>/dev/null || echo "$PUSH_RESPONSE3"
echo ""

echo "=========================================="
echo "测试完成！请检查哪个方式成功，并在手机上验证提醒效果。"
echo "=========================================="

