prgayman / larafcm
Laravel Firebase Cloud Messaging
Requires
- php: ^7.2.5|^8.0|^8.1
- guzzlehttp/guzzle: ^6.0|^7.2|^7.5
- illuminate/notifications: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
- monolog/monolog: ^1.12|^2.0|^3.0
README
简介
LaraFcm是一个易于使用的包,它可以通过Firebase Cloud Messaging (FCM)发送推送通知,与Laravel和Lumen兼容。
安装
要在项目中获取LaraFcm的最新版本,请从"composer"中要求它
$ composer require prgayman/larafcm
或者您可以直接在composer.json文件中添加它
{ "require": { "prgayman/larafcm": "1.0.0" } }
使用以下命令发布包的配置和迁移文件
$ php artisan vendor:publish --provider="Prgayman\LaraFcm\Providers\LaraFcmServiceProvider"
使用以下命令迁移larafcm_tokens
$ php artisan migrate
Laravel
直接在您的应用程序配置文件config/app.php中注册提供者 config/app.php
Laravel >= 5.5提供了包自动发现功能,感谢rasmuscnielsen和luiztessadri帮助在LaraFcm中实现此功能,因此提供者和外观的注册现在可能不再需要。
'providers' => [ Prgayman\LaraFcm\Providers\LaraFcmServiceProvider::class, ]
在同一个文件中添加外观别名
'aliases' => [ 'LaraFcm' => Prgayman\LaraFcm\Facades\LaraFcm::class, ]
Lumen
在您的bootstrap/app.php启动应用程序文件中注册提供者
在文件底部“注册服务提供者”部分添加以下行。
$app->register(Prgayman\LaraFcm\Providers\LaraFcmServiceProvider::class);
对于外观,在“创建应用程序”部分中添加以下行。
class_alias(\Prgayman\LaraFcm\Facades\LaraFcm::class, 'LaraFcm');
包配置
在您的.env文件中,添加Firebase Cloud Messaging的服务器密钥和密钥
LARAFCM_AUTHENTICATION_KEY=my_secret_server_key LARAFCM_SENDER_ID=my_secret_sender_id
要获取这些密钥,您必须在Firebase云消息控制台上创建一个新的应用程序。
在Firebase上创建您的应用程序后,您可以在项目设置 -> 云消息
中找到密钥。
用法
使用LaraFcm可以发送两种类型的消息
- 通知消息,有时也被称为“显示消息”
- 数据消息,由客户端应用程序处理
更多详细信息请参阅官方文档。
Larafcm令牌管理器 (Prgayman\LaraFcm\Services\LaraFcmToken)
use Prgayman\LaraFcm\Services\LaraFcmToken; // Store desvice token (new LaraFcmToken) ->setTokens(['token']) ->store(); // Store desvice token to specific user (new LaraFcmToken) ->setTokens('token') ->setModel(User::find(1)) ->store(); // Can you set platform or locale to token both options is optional (new LaraFcmToken) ->setTokens(['token']) ->setPlatform('android') ->setLocale('en') ->store(); /** * Get token from database you can use filter by model or locale ot platform to get tokens * * @param Illuminate\Database\Eloquent\Model|null $model * @param string|null $locale * @param string|null $platform * * @return array */ $tokens = LaraFcmToken::getDbTokens(); $removeTokens = []; /** * Remove toknes from database * @param array $tokens * * @return bool */ $isDeleted = LaraFcmToken::removeDbTokens($removeTokens);
Larafcm特性 HasLaraFcm (Prgayman\LaraFcm\Traits\HasLaraFcm)
use Illuminate\Foundation\Auth\User as Authenticatable; use Prgayman\LaraFcm\Traits\HasLaraFcm; // Add this line class User extends Authenticatable { use HasLaraFcm; // Add this line } $user = User::find(1); /** * Get user tokens can you use filter by locale or platform * * @param string|null $locale * @param string|null $platform * * @return array */ $locale = null; $platform = "ios"; $userTokens = $user->larafcmGetTokens($locale,$platform) /** * store user tokens can you set platform or locale to token * * @param array|string $tokens * @param string|null $locale * @param string|null $platform * * @return bool */ $storeUserTokens = ['new_token']; $user->larafcmStoreTokens($tokens, $locale, $platform)
下游消息
下游消息是一个通知消息、数据消息或两者,您通过其registration_Ids发送到目标设备或多个目标设备。
以下示例需要以下使用声明
use Prgayman\LaraFcm\Message\Options; use Prgayman\LaraFcm\Message\Data; use Prgayman\LaraFcm\Message\Notification; use Prgayman\LaraFcm\Message\Topics; use LaraFcm;
向设备发送下游消息
// Get all tokens form database return array you can set string token $tokens = LaraFcmToken::getDbTokens(); // Send Notifications without data $downstreamResponse = LaraFcm::to($tokens) notification( (new Notification) ->setTitle('New Order') ->setBody('You have placed order') ->setColor('#f00') ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->send(); // Send Notifications with data $downstreamResponse = LaraFcm::to($tokens) notification( (new Notification) ->setTitle('New Order') ->setBody('You have placed order') ->setColor('#f00') ) ->data( (new Data) ->addData(['key'=>"value"]) ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->send(); // Send data message $downstreamResponse = LaraFcm::to($tokens) ->data( (new Data) ->addData(['key'=>"value"]) ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->send(); $downstreamResponse->numberSuccess(); $downstreamResponse->numberFailure(); $downstreamResponse->numberModification(); // return Array - you must remove all this tokens in your database $downstreamResponse->tokensToDelete(); // return Array (key : oldToken, value : new token - you must change the token in your database) $downstreamResponse->tokensToModify(); // return Array - you should try to resend the message to the tokens in the array $downstreamResponse->tokensToRetry(); // return Array (key:token, value:error) - in production you should remove from your database the tokens $downstreamResponse->tokensWithError();
请参阅下游消息错误响应代码文档以获取更多信息。
向主题发送消息
$topicResponse = LaraFcm::notification( (new Notification) ->setTitle('New Order') ->setBody('You have placed order') ->setColor('#f00') ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->topics( (new Topics) ->topic('larafcm') ) ->send(); $topicResponse->isSuccess(); $topicResponse->shouldRetry(); $topicResponse->error();
向多个主题发送消息
它向以下主题注册的设备发送通知
- larafcm 和电商
- larafcm 和新闻
注意:主题支持每个表达式两个操作符
$topicResponse = LaraFcm::notification( (new Notification) ->setTitle('New Order') ->setBody('You have placed order') ->setColor('#f00') ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->topics( (new Topics) ->topic('larafcm') ->andTopic(function($condition) { $condition->topic('ecommerce')->orTopic('news'); }); ) ->send(); $topicResponse->isSuccess(); $topicResponse->shouldRetry(); $topicResponse->error());
选项
LaraFcm支持基于Firebase Cloud Messaging选项的选项。这些选项可以帮助您定义通知的特定性。
您可以按如下方式构建一个选项
use Prgayman\LaraFcm\Message\Options; $options = new Options; $options->setTimeToLive(42*60) ->setCollapseKey('a_collapse_key');
通知消息
通知有效负载用于发送通知,其行为由接收设备的应用状态和操作系统定义。
通知消息在应用处于后台时被发送到通知托盘。对于前台的应用程序,消息由以下回调处理
- didReceiveRemoteNotification:在iOS上
- 在Android上使用onMessageReceived()。数据包中的通知键包含通知。
查看官方文档。
use Prgayman\LaraFcm\Message\Notification; $notification = new Notification(); $notification->setTitle('title') ->setBody('body') ->setSound('sound') ->setBadge('badge');
通知与数据消息
当接收到包含通知和数据负载的消息时,应用的行为取决于应用是在后台还是在前台——基本上,取决于接收消息时应用是否处于活动状态(来源)。
- 后台,应用在通知托盘接收通知负载,并且只有在用户点击通知时才处理数据负载。
- 前台,您的应用会收到一个包含两个负载的消息对象。
主题
对于主题消息,LaraFcm提供了一套易于使用的API,它抽象了Firebase条件。为了在Firebase官方文档中给出的条件下执行,必须像以下这样使用LaraFcm:
官方文档条件
'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
use Prgayman\LaraFcm\Message\Topics; $topics = new Topics; $topics->topic('TopicA') ->andTopic(function($condition) { $condition->topic('TopicB')->orTopic('TopicC'); });
辅助函数
larafcm()
$topicResponse = larafcm() ->notification( (new Notification) ->setTitle('New Order') ->setBody('You have placed order') ->setColor('#f00') ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->data( (new Data) ->addData(['key'=>"value"]) ) ->topics( (new Topics) ->topic('larafcm') ) ->send();
Laravel通知通道
use Illuminate\Notifications\Notification; use Prgayman\LaraFcm\Services\LaraFcm; use Prgayman\LaraFcm\Message\Data; use Prgayman\LaraFcm\Message\Notification as LaraFcmNotification; use Prgayman\LaraFcm\Message\Options; class SendPlacedOrder extends Notification { public function via($notifiable) { return ['larafcm']; } public function toLaraFcm($notifiable) { $tokens = ['TOKEN_1','TOKEN_2']; return (new LaraFcm) ->notification( (new LaraFcmNotification) ->setTitle('New Order') ->setBody('You have placed order') ->setColor('#f00') ) ->options( (new Options) ->setTimeToLive(60*20) ->setContentAvailable(true) ) ->data( (new Data) ->addData(['key'=>"value"]) ) ->to($tokens) ->send(); } }
许可证
这个库是开源软件,许可协议为MIT许可。
本文档的部分内容来自官方文档。您可以在Firebase云消息传递网站上找到完整内容。