juniorlemes / firebase-cloud-messaging-http-v1-php
Firebase 云消息 HTTP v1 PHP
0.1.0
2024-09-27 02:34 UTC
Requires
- php: ^7.0.0
- firebase/php-jwt: ^5.0
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- laravel/pint: ^1.13.1
- pestphp/pest: ^2.19.2
- phpstan/phpstan: ^1.10.34
- rector/rector: ^0.18.3
- symfony/var-dumper: ^6.3.4
README
Firebase 云消息 HTTP v1 for PHP
安装
composer require juniorlemes/firebase-cloud-messaging-http-v1-php
发送消息
<?php $authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true); $projectID = 'my-project-1'; $body = [ 'message' => [ 'token' => '<token:string>', 'notification' => [ 'title' => 'Breaking News', 'body' => 'New news story available.', ], 'data' => [ 'story_id' => 'story_12345', ], ], ]; $bearerToken = FCM::getBearerToken($authKeyContent); FCM::send($bearerToken, $projectID, $body);
发送多条消息
<?php $authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true); $projectID = 'my-project-1'; $tokens = [ '<token1:string>', '<token2:string>', '<token3:string>', ]; $bearerToken = FCM::getBearerToken($authKeyContent); foreach ($tokens as $token) { $body = [ 'message' => [ 'token' => $token, 'notification' => [ 'title' => 'Breaking News', 'body' => 'New news story available.', ], 'data' => [ 'story_id' => 'story_12345', ], ], ]; FCM::send($bearerToken, $projectID, $body); }
订阅主题
<?php $authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true); $tokens = [ '<token1:string>', '<token2:string>', '<token3:string>', ]; $bearerToken = FCM::getBearerToken($authKeyContent); FCM::subscribeToTopic($bearerToken, 'my-topic-1', $tokens);
取消订阅主题
<?php $authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true); $tokens = [ '<token1:string>', '<token2:string>', '<token3:string>', ]; $bearerToken = FCM::getBearerToken($authKeyContent); FCM::unsubscribeFromTopic($bearerToken, 'my-topic-1', $tokens);