sinergy84 / push-notification-php-library
向android|ios设备发送推送通知,支持APNs、FCM
v1.4.4
2023-10-10 10:12 UTC
Requires
- php: >=8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.4
- vlucas/phpdotenv: ^5.5
README
一个用于轻松推送设备消息通知的独立PHP库。
欢迎贡献力量!
安装
composer require sinergy84/push-notification-php-library
composer dump-autoload -o
此仓库使用PSR-0自动加载。使用composer安装后,如需调整,请修改自动加载配置,或在index.php中包含vendor/autoload.php。
要求
- PHP 5.6+
- PHP Curl和OpenSSL模块
支持的版本
- APNS (苹果)
- GCM (安卓)和FCM (安卓)
设置
-
在.env文件中设置你的提供商设置(Apn, Fcm)(确保将.env.example重命名为.env并填写所有要求)
-
到.env文件的路径(可选):如果.env文件不在库的根目录中,请将自定义的$path参数设置为.env文件,在
src/PushNotification/Setting::__construct($path = NULL)
中
如何使用
include_once "vendor/autoload.php"; use PushNotification\Service\PushService; use PushNotification\Settings; $data = array( 'device' => array( 'name' => '', // Android or AppleIOS 'token' => '', // device token | user token , if you want to send to apple device you have to fill this 'id' => 'unique id here'), 'message' => array( 'action' => 'test', 'title' => 'this is test title', 'targets' => array(''), // if you want to use Fcm you can inclue array of targets 'body' => 'this is body', 'type' => '', // AndroidMessages or IOSMessages 'data' => array('type' => 'testType')) ); // new Settings('path_to_env_file_if_not_library_root_dir'); new Settings(); $response = PushService::getInstance()->send($data);
安卓
include_once "vendor/autoload.php"; use PushNotification\Service\PushService; $data = array( 'device' => array( 'name' => 'Android', 'token' => '', 'id' => 'some id here '), 'message' => array( 'action' => 'test', 'title' => 'this is test title', 'targets' => array('token1', 'token2', 'token3'), 'body' => 'this is body', 'type' => 'AndroidMessages', 'data' => array('type' => 'testType')) ); // new Settings('path_to_env_file_if_not_library_root_dir'); new Settings(); $response = PushService::getInstance()->send($data); print_r($response);
IOS
include_once "vendor/autoload.php"; use PushNotification\Service\PushService; $data = array( 'device' => array( 'name' => 'AppleIOS', 'token' => 'token', 'id' => 'BECDSx'), 'message' => array( 'action' => 'test', 'title' => 'this is test title', 'targets' => array(), 'body' => 'this is body', 'type' => 'IOSMessages', 'data' => array('type' => 'testType')) ); // new Settings('path_to_env_file_if_not_library_root_dir'); new Settings(); $response = PushService::getInstance()->send($data); print_r($response);