php [onesignal] onesignal php api

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [onesignal] onesignal php api相关的知识,希望对你有一定的参考价值。

<?php
//send message
function sendMessage(){
    $content = array(
        "en" => 'Testing Message'
        );

    $fields = array(
        'app_id' => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
        'included_segments' => array('All'),
        'data' => array("foo" => "bar"),
        'large_icon' =>"ic_launcher_round.png",
        'contents' => $content
    );

    $fields = json_encode($fields);
    print("\nJSON sent:\n");
    print($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
<?php
/*
https://github.com/norkunas/onesignal-php-api
*/
use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Client\Common\HttpMethodsClient as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use OneSignal\Config;
use OneSignal\OneSignal;

/**
	configuration
*/    
$config = new Config();
$config->setApplicationId('your_application_id');
$config->setApplicationAuthKey('your_application_auth_key');
$config->setUserAuthKey('your_auth_key');

$guzzle = new GuzzleClient([ // http://docs.guzzlephp.org/en/stable/quickstart.html
    // ..config
]);

$client = new HttpClient(new GuzzleAdapter($guzzle), new GuzzleMessageFactory());
$api = new OneSignal($config, $client);

/**
	Application
*/
//details of all of your current OneSignal applications
$myApps = $api->apps->getAll();

//get single app
$myApp = $api->apps->getOne('application_id');

/**
	Devices API
*/
//details of multiple devices
$devices = $api->devices->getAll();

//details of an existing device
$device = $api->devices->getOne('device_id');

//Register a new device
$newDevice = $api->devices->add([
    'device_type' => Devices::ANDROID,
    'identifier' => 'abcdefghijklmn',
]);

//Update an existing device
$api->devices->update('device_id', [
    'session_count' => 2,
]);

/**
	Notifications API
*/
//details of multiple notifications
$notifications = $api->notifications->getAll();

//Get the details of a single notification
$notification = $api->notifications->getOne('notification_id');

//Create and send notifications or emails to a segment or individual users
$api->notifications->add([
    'contents' => [
        'en' => 'Notification message'
    ],
    'included_segments' => ['All'],
    'data' => ['foo' => 'bar'],
    'isChrome' => true,
    'send_after' => new \DateTime('1 hour'),
    'filters' => [
        [
            'field' => 'tag',
            'key' => 'is_vip',
            'relation' => '!=',
            'value' => 'true',
        ],
        [
            'operator' => 'OR',
        ],
        [
            'field' => 'tag',
            'key' => 'is_admin',
            'relation' => '=',
            'value' => 'true',
        ],
    ],
    // ..other options
]));

//Mark notification as opened
$api->notifications->open('notification_id');

//Stop a scheduled or currently outgoing notification
$api->notifications->cancel('notification_id');

以上是关于php [onesignal] onesignal php api的主要内容,如果未能解决你的问题,请参考以下文章

如何通过移动设备首选项设置过滤 OneSignal 推送消息

OneSignal 在 Web 通知上发送 js 变量

dyld:库未加载:@rpath/OneSignal.framework/OneSignal

无法解决:原生 Android 上的 com.onesignal:onesignal:3.12.3

在 android 上未收到 OneSignal 通知,但从 OneSignal 控制台发送

OneSignal在Web通知上发送js变量