You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本运行成功但OneSignal仪表盘无通知显示求助

排查OneSignal通知发送后仪表盘无显示问题

我是编程新手,正在开展一项校园项目,让树莓派通过Python脚本向OneSignal发送通知。按下回车键后终端提示通知已发送,但OneSignal仪表盘无内容显示,已尝试刷新页面,请求协助排查问题。

代码实现

import requests
from pynput import keyboard
    
ONESIGNAL_APP_ID = 'appid'
ONESIGNAL_REST_API_KEY = 'apikey'
ONESIGNAL_API_URL = 'https://onesignal.com/api/v1/notifications'

def send_notification(message):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Basic {ONESIGNAL_REST_API_KEY}'
    }

    payload = {
        'app_id': ONESIGNAL_APP_ID,
        'contents': {'en': message},
        'included_segments': ['All']
    }

    print("Sending payload:", payload)

    response = requests.post(ONESIGNAL_API_URL, headers=headers, json=payload)

    print("Response status code:", response.status_code)
    print("Response content:", response.content)

    if response.status_code == 200:
        print('Notification sent successfully')
    else:
        print(f'Failed to send notification. Status code: {response.status_code}')

def on_return_key_press(key):
    if key == keyboard.Key.enter:
        print('Enter key pressed. Sending notification...')
        send_notification('Robert is feeling stressed')

with keyboard.Listener(on_press=on_return_key_press) as listener:
    print('Listener activated. Press Enter to send notification.')
    listener.join()

排查步骤

  • 核对密钥与App ID
    确认ONESIGNAL_APP_ID和ONESIGNAL_REST_API_KEY是从OneSignal控制台Settings > Keys & IDs中准确复制的,避免多余空格或引号错误。REST API Key是单独的字段,不要和App ID混淆。

  • 解析响应的详细信息
    代码中仅打印了原始响应内容,建议添加JSON解析代码,查看更清晰的反馈:

    import json
    # 在print("Response content:", response.content)后添加
    try:
        parsed_response = json.loads(response.content)
        print("Parsed response:", parsed_response)
    except:
        print("Failed to parse response")
    

    即使状态码为200,OneSignal也可能返回通知未送达的原因(比如无匹配用户)。

  • 确认应用存在活跃订阅用户
    如果你的OneSignal应用还没有任何设备订阅,发送给All分段的通知不会在仪表盘显示——因为没有接收目标。先在测试设备上完成订阅后再重试。

  • 调整仪表盘过滤条件
    在OneSignal的Notifications页面,切换过滤条件为"All Notifications",默认过滤可能隐藏无接收者的通知。

  • 验证请求格式合规性
    检查app_id是否为UUID格式,contents字段的语言设置是否与应用支持的语言匹配(比如应用只支持中文则需添加zh字段)。

  • 用外部工具测试API
    用curl直接发送请求,排除代码环境问题:

    curl --request POST \
      --url https://onesignal.com/api/v1/notifications \
      --header 'Authorization: Basic YOUR_REST_API_KEY' \
      --header 'Content-Type: application/json' \
      --data '{
        "app_id": "YOUR_APP_ID",
        "contents": {"en": "Test notification"},
        "included_segments": ["All"]
      }'
    

    对比工具返回的结果和代码中的响应,定位差异。

内容的提问来源于stack exchange,提问作者Robert Brennan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 21:19:56