项目作者: yusefarianpour

项目描述 :
The Laravel FCM Channel
高级语言: PHP
项目地址: git://github.com/yusefarianpour/the-laravel-fcm-channel.git
创建时间: 2017-10-11T06:42:15Z
项目社区:https://github.com/yusefarianpour/the-laravel-fcm-channel

开源协议:

下载


The Laravel FCM Channel

The Laravel Firebase Cloud Messaging(FCM) Notification Channel

Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.3+ required.

Install

This package can be installed through Composer.

  1. composer require journalctl/laravel-to-fcm

Add your Firebase API Key to config/services.php

  1. 'fcm' => [
  2. 'key' => 'Your Firebase Cloud Messaging token',
  3. ],

Example Usage

Use Artisan to create a notification:

  1. php artisan make:notification SomeNotification

Return [fcm] in the public function via($notifiable) method of your notification:

  1. public function via($notifiable)
  2. {
  3. return ['fcm'];
  4. }

Or :

  1. use Journalctl\Channels\FirebaseChannel;
  2. ...
  3. public function via($notifiable)
  4. {
  5. return [FirebaseChannel::class];
  6. }

Add the method public function toFcm($notifiable) to your notification, and return an instance of FirebaseMessage:

  1. use Journalctl\Channels\FirebaseChannel;
  2. use Journalctl\Channels\FirebaseMessage;
  3. ...
  4. public function toFcm($notifiable)
  5. {
  6. $message = new FirebaseMessage();
  7. $message
  8. ->title('Foo') // Required
  9. ->body('Bar') // Required
  10. ->sound() // Optional
  11. ->icon() // Optional
  12. ->clickAction(); // Optional
  13. $message->data([
  14. 'param1' => 'baz' // Optional
  15. ])->priority(FirebaseMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
  16. return $message;
  17. }

When sending to specific device, make sure your notifiable entity has routeNotificationForFcm method defined:

  1. /**
  2. * Route notifications for the Firebase Cloud Messaging channel.
  3. *
  4. * @return string
  5. */
  6. public function routeNotificationForFcm()
  7. {
  8. return $this->device_token;
  9. }

License

The “The Laravel FCM Channel” is open-sourced software licensed under the MIT license.