可用 SDK
安装
- Node.js
- Python
- Java
- Go
- PHP
- Ruby
- C# (.NET)
- Rust
复制
询问AI
npm install @onesignal/node-onesignal
复制
询问AI
pip install onesignal-python-api
MavenGradle
复制
询问AI
<dependency>
<groupId>com.onesignal</groupId>
<artifactId>onesignal-java-client</artifactId>
<version>5.3.0</version>
</dependency>
复制
询问AI
implementation "com.onesignal:onesignal-java-client:5.3.0"
复制
询问AI
go get github.com/OneSignal/onesignal-go-api/v5
添加到 然后运行
composer.json:复制
询问AI
{
"require": {
"onesignal/onesignal-php-api": "*@dev"
}
}
composer update。添加到您的 然后运行
Gemfile:复制
询问AI
gem 'onesignal', '~> 5.3.0-beta1'
bundle install。复制
询问AI
dotnet add package OneSignalApi
添加到
Cargo.toml 的 [dependencies] 下:复制
询问AI
onesignal-rust-api = "5.3.0"
配置
每个 SDK 都需要通过 API 密钥进行身份验证。有两种密钥类型:- REST API 密钥 — 大多数端点(发送通知、管理用户等)都需要。在您应用的设置 > 密钥和 ID 中找到。
- 组织 API 密钥 — 仅用于组织级端点,如创建或列出应用程序。在组织设置中找到。
- Node.js
- Python
- Java
- Go
- PHP
- Ruby
- C# (.NET)
- Rust
复制
询问AI
const OneSignal = require('@onesignal/node-onesignal');
const configuration = OneSignal.createConfiguration({
restApiKey: 'YOUR_REST_API_KEY',
organizationApiKey: 'YOUR_ORGANIZATION_API_KEY',
});
const client = new OneSignal.DefaultApi(configuration);
复制
询问AI
import onesignal
from onesignal.api import default_api
configuration = onesignal.Configuration(
rest_api_key='YOUR_REST_API_KEY',
organization_api_key='YOUR_ORGANIZATION_API_KEY',
)
with onesignal.ApiClient(configuration) as api_client:
client = default_api.DefaultApi(api_client)
复制
询问AI
import com.onesignal.client.ApiClient;
import com.onesignal.client.Configuration;
import com.onesignal.client.auth.HttpBearerAuth;
import com.onesignal.client.api.DefaultApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
HttpBearerAuth restApiAuth = (HttpBearerAuth) defaultClient
.getAuthentication("rest_api_key");
restApiAuth.setBearerToken("YOUR_REST_API_KEY");
HttpBearerAuth orgApiAuth = (HttpBearerAuth) defaultClient
.getAuthentication("organization_api_key");
orgApiAuth.setBearerToken("YOUR_ORGANIZATION_API_KEY");
DefaultApi client = new DefaultApi(defaultClient);
复制
询问AI
import onesignal "github.com/OneSignal/onesignal-go-api"
restAuth := context.WithValue(
context.Background(),
onesignal.RestApiKey,
"YOUR_REST_API_KEY",
)
orgAuth := context.WithValue(
restAuth,
onesignal.OrganizationApiKey,
"YOUR_ORGANIZATION_API_KEY",
)
apiClient := onesignal.NewAPIClient(onesignal.NewConfiguration())
复制
询问AI
use onesignal\client\api\DefaultApi;
use onesignal\client\Configuration;
use GuzzleHttp;
$config = Configuration::getDefaultConfiguration()
->setRestApiKeyToken('YOUR_REST_API_KEY')
->setOrganizationApiKeyToken('YOUR_ORGANIZATION_API_KEY');
$client = new DefaultApi(
new GuzzleHttp\Client(),
$config
);
复制
询问AI
require 'onesignal'
OneSignal.configure do |config|
config.rest_api_key = 'YOUR_REST_API_KEY'
config.organization_api_key = 'YOUR_ORGANIZATION_API_KEY'
end
client = OneSignal::DefaultApi.new
复制
询问AI
using OneSignalApi.Api;
using OneSignalApi.Client;
var config = new Configuration();
config.BasePath = "https://api.onesignal.com";
config.AccessToken = "YOUR_REST_API_KEY";
var client = new DefaultApi(config);
复制
询问AI
use onesignal::apis::configuration::Configuration;
fn create_configuration() -> Configuration {
let mut config = Configuration::new();
config.rest_api_key_token = Some("YOUR_REST_API_KEY".to_string());
config.organization_api_key_token = Some("YOUR_ORGANIZATION_API_KEY".to_string());
config
}
将 API 密钥存储在环境变量或密钥管理器中。切勿将它们提交到源代码控制中。
发送推送通知
通过定向至分段,向网页和移动端订阅发送推送通知。- Node.js
- Python
- Java
- Go
- PHP
- Ruby
- C# (.NET)
- Rust
复制
询问AI
const notification = new OneSignal.Notification();
notification.app_id = 'YOUR_APP_ID';
notification.contents = { en: 'Hello from OneSignal!' };
notification.headings = { en: 'Push Notification' };
notification.included_segments = ['Subscribed Users'];
const response = await client.createNotification(notification);
console.log('Notification ID:', response.id);
复制
询问AI
notification = onesignal.Notification(
app_id='YOUR_APP_ID',
contents=onesignal.StringMap(en='Hello from OneSignal!'),
headings=onesignal.StringMap(en='Push Notification'),
included_segments=['Subscribed Users'],
)
response = client.create_notification(notification)
print('Notification ID:', response.id)
复制
询问AI
import com.onesignal.client.model.Notification;
import com.onesignal.client.model.StringMap;
Notification notification = new Notification();
notification.setAppId("YOUR_APP_ID");
StringMap contents = new StringMap();
contents.en("Hello from OneSignal!");
notification.setContents(contents);
StringMap headings = new StringMap();
headings.en("Push Notification");
notification.setHeadings(headings);
notification.setIncludedSegments(Arrays.asList("Subscribed Users"));
var response = client.createNotification(notification);
System.out.println("Notification ID: " + response.getId());
复制
询问AI
notification := *onesignal.NewNotification("YOUR_APP_ID")
notification.SetContents(onesignal.StringMap{En: onesignal.PtrString("Hello from OneSignal!")})
notification.SetHeadings(onesignal.StringMap{En: onesignal.PtrString("Push Notification")})
notification.SetIncludedSegments([]string{"Subscribed Users"})
response, _, err := apiClient.DefaultApi
.CreateNotification(orgAuth)
.Notification(notification)
.Execute()
if err != nil {
log.Fatal(err)
}
fmt.Println("Notification ID:", response.GetId())
复制
询问AI
use onesignal\client\model\Notification;
use onesignal\client\model\StringMap;
$content = new StringMap();
$content->setEn('Hello from OneSignal!');
$headings = new StringMap();
$headings->setEn('Push Notification');
$notification = new Notification();
$notification->setAppId('YOUR_APP_ID');
$notification->setContents($content);
$notification->setHeadings($headings);
$notification->setIncludedSegments(['Subscribed Users']);
$response = $client->createNotification($notification);
echo 'Notification ID: ' . $response->getId();
复制
询问AI
notification = OneSignal::Notification.new({
app_id: 'YOUR_APP_ID',
contents: { en: 'Hello from OneSignal!' },
headings: { en: 'Push Notification' },
included_segments: ['Subscribed Users']
})
response = client.create_notification(notification)
puts "Notification ID: #{response.id}"
复制
询问AI
using OneSignalApi.Model;
var notification = new Notification(appId: "YOUR_APP_ID")
{
Contents = new StringMap(en: "Hello from OneSignal!"),
Headings = new StringMap(en: "Push Notification"),
IncludedSegments = new List<string> { "Subscribed Users" }
};
var response = client.CreateNotification(notification);
Console.WriteLine("Notification ID: " + response.Id);
复制
询问AI
use onesignal::apis::default_api;
use onesignal::models::{Notification, StringMap};
let mut contents = StringMap::new();
contents.en = Some("Hello from OneSignal!".to_string());
let mut headings = StringMap::new();
headings.en = Some("Push Notification".to_string());
let mut notification = Notification::new("YOUR_APP_ID".to_string());
notification.contents = Some(Box::new(contents));
notification.headings = Some(Box::new(headings));
notification.included_segments = Some(vec!["Subscribed Users".to_string()]);
let config = create_configuration();
let response = default_api::create_notification(&config, notification).await;
发送电子邮件
通过email 渠道向订阅发送电子邮件。
- Node.js
- Python
- Java
- Go
- PHP
- Ruby
- C# (.NET)
- Rust
复制
询问AI
const notification = new OneSignal.Notification();
notification.app_id = 'YOUR_APP_ID';
notification.email_subject = 'Important Update';
notification.email_body = '<h1>Hello!</h1><p>This is an HTML email.</p>';
notification.included_segments = ['Subscribed Users'];
notification.channel_for_external_user_ids = 'email';
const response = await client.createNotification(notification);
复制
询问AI
notification = onesignal.Notification(
app_id='YOUR_APP_ID',
email_subject='Important Update',
email_body='<h1>Hello!</h1><p>This is an HTML email.</p>',
included_segments=['Subscribed Users'],
channel_for_external_user_ids='email',
)
response = client.create_notification(notification)
复制
询问AI
Notification notification = new Notification();
notification.setAppId("YOUR_APP_ID");
notification.setEmailSubject("Important Update");
notification.setEmailBody("<h1>Hello!</h1><p>This is an HTML email.</p>");
notification.setIncludedSegments(Arrays.asList("Subscribed Users"));
notification.setChannelForExternalUserIds("email");
var response = client.createNotification(notification);
复制
询问AI
notification := *onesignal.NewNotification("YOUR_APP_ID")
notification.SetEmailSubject("Important Update")
notification.SetEmailBody("<h1>Hello!</h1><p>This is an HTML email.</p>")
notification.SetIncludedSegments([]string{"Subscribed Users"})
notification.SetChannelForExternalUserIds("email")
response, _, err := apiClient.DefaultApi
.CreateNotification(orgAuth)
.Notification(notification)
.Execute()
复制
询问AI
$notification = new Notification();
$notification->setAppId('YOUR_APP_ID');
$notification->setEmailSubject('Important Update');
$notification->setEmailBody('<h1>Hello!</h1><p>This is an HTML email.</p>');
$notification->setIncludedSegments(['Subscribed Users']);
$notification->setChannelForExternalUserIds('email');
$response = $client->createNotification($notification);
复制
询问AI
notification = OneSignal::Notification.new({
app_id: 'YOUR_APP_ID',
email_subject: 'Important Update',
email_body: '<h1>Hello!</h1><p>This is an HTML email.</p>',
included_segments: ['Subscribed Users'],
channel_for_external_user_ids: 'email'
})
response = client.create_notification(notification)
复制
询问AI
var notification = new Notification(appId: "YOUR_APP_ID")
{
EmailSubject = "Important Update",
EmailBody = "<h1>Hello!</h1><p>This is an HTML email.</p>",
IncludedSegments = new List<string> { "Subscribed Users" },
ChannelForExternalUserIds = "email"
};
var response = client.CreateNotification(notification);
复制
询问AI
let mut notification = Notification::new("YOUR_APP_ID".to_string());
notification.email_subject = Some("Important Update".to_string());
notification.email_body = Some("<h1>Hello!</h1><p>This is an HTML email.</p>".to_string());
notification.included_segments = Some(vec!["Subscribed Users".to_string()]);
notification.channel_for_external_user_ids = Some("email".to_string());
let response = default_api::create_notification(&config, notification).await;
发送短信
通过sms 渠道向订阅发送短信。
- Node.js
- Python
- Java
- Go
- PHP
- Ruby
- C# (.NET)
- Rust
复制
询问AI
const notification = new OneSignal.Notification();
notification.app_id = 'YOUR_APP_ID';
notification.contents = { en: 'Your SMS message content here' };
notification.included_segments = ['Subscribed Users'];
notification.channel_for_external_user_ids = 'sms';
notification.sms_from = '+15551234567';
const response = await client.createNotification(notification);
复制
询问AI
notification = onesignal.Notification(
app_id='YOUR_APP_ID',
contents=onesignal.StringMap(en='Your SMS message content here'),
included_segments=['Subscribed Users'],
channel_for_external_user_ids='sms',
sms_from='+15551234567',
)
response = client.create_notification(notification)
复制
询问AI
StringMap contents = new StringMap();
contents.en("Your SMS message content here");
Notification notification = new Notification();
notification.setAppId("YOUR_APP_ID");
notification.setContents(contents);
notification.setIncludedSegments(Arrays.asList("Subscribed Users"));
notification.setChannelForExternalUserIds("sms");
notification.setSmsFrom("+15551234567");
var response = client.createNotification(notification);
复制
询问AI
notification := *onesignal.NewNotification("YOUR_APP_ID")
notification.SetContents(onesignal.StringMap{En: onesignal.PtrString("Your SMS message content here")})
notification.SetIncludedSegments([]string{"Subscribed Users"})
notification.SetChannelForExternalUserIds("sms")
notification.SetSmsFrom("+15551234567")
response, _, err := apiClient.DefaultApi
.CreateNotification(orgAuth)
.Notification(notification)
.Execute()
复制
询问AI
$content = new StringMap();
$content->setEn('Your SMS message content here');
$notification = new Notification();
$notification->setAppId('YOUR_APP_ID');
$notification->setContents($content);
$notification->setIncludedSegments(['Subscribed Users']);
$notification->setChannelForExternalUserIds('sms');
$notification->setSmsFrom('+15551234567');
$response = $client->createNotification($notification);
复制
询问AI
notification = OneSignal::Notification.new({
app_id: 'YOUR_APP_ID',
contents: { en: 'Your SMS message content here' },
included_segments: ['Subscribed Users'],
channel_for_external_user_ids: 'sms',
sms_from: '+15551234567'
})
response = client.create_notification(notification)
复制
询问AI
var notification = new Notification(appId: "YOUR_APP_ID")
{
Contents = new StringMap(en: "Your SMS message content here"),
IncludedSegments = new List<string> { "Subscribed Users" },
ChannelForExternalUserIds = "sms",
SmsFrom = "+15551234567"
};
var response = client.CreateNotification(notification);
复制
询问AI
let mut contents = StringMap::new();
contents.en = Some("Your SMS message content here".to_string());
let mut notification = Notification::new("YOUR_APP_ID".to_string());
notification.contents = Some(Box::new(contents));
notification.included_segments = Some(vec!["Subscribed Users".to_string()]);
notification.channel_for_external_user_ids = Some("sms".to_string());
notification.sms_from = Some("+15551234567".to_string());
let response = default_api::create_notification(&config, notification).await;
完整 API 参考
每个服务器 SDK 支持相同的 API 端点集。请参阅 SDK 的 API 文档获取完整方法列表,包括用户、订阅、分段、模板等。| SDK | API 参考 |
|---|---|
| Node.js | DefaultApi.md |
| Python | DefaultApi.md |
| Java | DefaultApi.md |
| Go | DefaultApi.md |
| PHP | DefaultApi.md |
| Ruby | DefaultApi.md |
| C# (.NET) | DefaultApi.md |
| Rust | default_api docs |
| C++ | GitHub |