• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

benwilkins/laravel-fcm-notification: Laravel FCM (Firebase Cloud Messaging) Noti ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

benwilkins/laravel-fcm-notification

开源软件地址(OpenSource Url):

https://github.com/benwilkins/laravel-fcm-notification

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Laravel FCM Notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel

GitHub tag Packagist Downloads Build Status License

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

This package works only with Legacy HTTP Server Protocol

Install

This package can be installed through Composer.

composer require benwilkins/laravel-fcm-notification

If installing on < Laravel 5.5 then add the service provider:

// config/app.php
'providers' => [
    ...
    Benwilkins\FCM\FcmNotificationServiceProvider::class,
    ...
];

Add your Firebase API Key in config/services.php.

return [
   
    ...
    ...
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'key' => env('FCM_SECRET_KEY')
     ]
];

Example Usage

Use Artisan to create a notification:

php artisan make:notification SomeNotification

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

public function via($notifiable)
{
    return ['fcm'];
}

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

use Benwilkins\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

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

/**
 * Route notifications for the FCM channel.
 *
 * @param  \Illuminate\Notifications\Notification  $notification
 * @return string
 */
public function routeNotificationForFcm($notification)
{
    return $this->device_token;
}

When sending to a topic, you may define so within the toFcm method in the notification:

use Benwilkins\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->to('the-topic', $recipientIsTopic = true)
    ->content([...])
    ->data([...]);
    
    return $message;
}

Or when sending with a condition:

use Benwilkins\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->contentAvailable(true)
        ->priority('high')
        ->condition("'user_".$notifiable->id."' in topics")
        ->data([...]);
    
    return $message;
}

You may provide optional headers or override the request headers using setHeaders():

use Benwilkins\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->setHeaders([
        'project_id'    =>  "48542497347"   // FCM sender_id
    ])->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

Interpreting a Response

To process any laravel notification channel response check Laravel Notification Events

This channel return a json array response:

 {
    "multicast_id": "number",
    "success": "number",
    "failure": "number",
    "canonical_ids": "number",
    "results": "array"
 }

Check FCM Legacy HTTP Server Protocol for response interpreting documentation.

License

The MIT License (MIT). Please see License File for more information.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap