使用Node.js+OneSignal创建通知失败,无用户设备是否是原因?
问题描述
我用Node.js调用OneSignal API创建通知,代码如下:
const configuration = OneSignal.createConfiguration({ appId: '640e4a33-7e4c-4ca1-8ca2-5fe11a7d1058', appKey: 'ZjMzYjUyNzktNjFjNi00MDQzLWE1MmEtNzk3NDliMWU0MmIw', }); const client = new OneSignal.DefaultApi(configuration); const notification = new OneSignal.Notification(); notification.app_id = "640e4a33-7e4c-4ca1-8ca2-5fe11a7d1058"; // Name property may be required in some case, for instance when sending an SMS. notification.name = "test_notification_name"; notification.contents = { en: "English message", es: "Spanish message" } // required for Huawei notification.headings = { en: "Gig'em Ags" } notification.included_segments = ["All"]; let testCreateNotificaiton = await client.createNotification(notification); console.log(testCreateNotificaiton)
控制台返回:
CreateNotificaitonSuccessResponse { id : "", errors:Notification200Errors{} }
我不清楚问题出在哪,想请人帮忙排查。另外,我的OneSignal后台没有任何用户或设备,这会不会导致这个问题?
排查分析
核心原因:无目标设备/用户
OneSignal返回空ID且无错误,大概率是因为你选择发送到All分段,但该分段下没有任何注册的设备或用户。OneSignal在这种情况下会生成“空通知”,不会实际推送,所以返回空ID,同时因为未触发错误逻辑,errors字段为空。其他排查点
- API密钥校验:确认
appKey(即REST API Key)是否正确,且拥有创建通知的权限,可去OneSignal后台Settings > Keys & IDs页面核对。 - 代码冗余处理:你已在
createConfiguration中传入appId,无需再给notification.app_id重复赋值,虽不会直接引发问题,但建议简化代码。 - 异步上下文确认:确保
await client.createNotification(notification)是在async函数内执行,否则会抛出语法错误,你的代码已使用await,此点大概率已处理。
- API密钥校验:确认
验证方案
先在OneSignal后台注册一个测试设备(比如通过测试APP或网页SDK添加测试用户),再重新运行代码,此时应能返回有效通知ID,且后台可看到通知记录。
内容的提问来源于stack exchange,提问作者Ahmad Raid
相关产品推荐
相关产品推荐

