davidaldan / laravel-fcm

Laravel / Lumen 的 Firebase Cloud Messaging (FCM) 包

1.0.1 2021-04-16 17:54 UTC

This package is auto-updated.

Last update: 2024-09-17 01:56:58 UTC


README

介绍

Laravel-FCM 是一个易于使用的包,它支持 Laravel 和 Lumen,用于通过 Firebase Cloud Messaging (FCM) 发送推送通知。

目前它仅支持通过 HTTP 协议进行以下操作:

  • 向一个或多个设备发送下行消息
  • 管理群组并向群组发送消息
  • 发送主题消息

安装

Composer json 文件

$ "davidaldan/laravel-fcm": "0.0.*"

Laravel

直接在您的 app 配置文件 config/app.php 中注册提供者

'providers' => [
	// ...

	LaravelFCM\FCMServiceProvider::class,
]

在同一文件中添加外观别名

'aliases' => [
	...
	'FCM'      => LaravelFCM\Facades\FCM::class,
	'FCMGroup' => LaravelFCM\Facades\FCMGroup::class, // Optional
]

使用以下命令发布包配置文件

$ php artisan vendor:publish --provider="LaravelFCM\FCMServiceProvider"

包配置

在您的 .env 文件中,添加 Firebase Cloud Messaging 的服务器密钥和密钥

FCM_SERVER_KEY=my_secret_server_key
FCM_SENDER_ID=my_secret_sender_id

下行消息

下行消息是指通过其 registration_Ids 向目标设备或多个目标设备发送的通知消息或数据消息。

以下示例需要以下使用声明

use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use LaravelFCM\Message\PayloadApnsBuilder;
use FCM;

向设备发送下行消息

$optionBuilder = new OptionsBuilder();
$optionBuilder->setTimeToLive(60*20);

$notificationBuilder = new PayloadNotificationBuilder('my title');
$notificationBuilder->setBody('Hello world')
				    ->setSound('default');

$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData([
	'a_data' 					=> 'my_data', //extra data can be array and with json_encode()
	'notification_foreground'	=> "true" //true-false
]);

$option = $optionBuilder->build();
$notification = $notificationBuilder->build();
$data = $dataBuilder->build();

$token = "a_registration_from_your_database";

$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

$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();

向多个设备发送下行消息

$optionBuilder = new OptionsBuilder();
$optionBuilder->setTimeToLive(60*20);

$notificationBuilder = new PayloadNotificationBuilder('my title');
$notificationBuilder->setBody('Hello world')
				    ->setSound('default');

$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData(['a_data' => 'my_data']);

$option = $optionBuilder->build();
$notification = $notificationBuilder->build();
$data = $dataBuilder->build();

// You must change it to get your tokens
$tokens = MYDATABASE::pluck('fcm_token')->toArray();

$downstreamResponse = FCM::sendTo($tokens, $option, $notification, $data);

$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 present in this array
$downstreamResponse->tokensWithError();

通知声音

Android

->setChannelId('services_channel_id')

iOS

$apnsBuilder = new PayloadApnsBuilder();
$apnsBuilder->addData([
	'payload' => [
		'aps' => [
			'sound'=>'default'
		]
	]
]);

许可

此库是开源软件,根据 MIT 许可证 许可。

部分文档来自官方文档。您可以在 Firebase Cloud Messaging 网站 上找到完整的文档。