GithubHelp home page GithubHelp logo

kawankoding / laravel-fcm Goto Github PK

View Code? Open in Web Editor NEW
172.0 10.0 56.0 74 KB

Firebase Cloud Messaging (FCM) sender for Laravel

PHP 100.00%
fcm notifications firebase cloud-messaging laravel laravel-fcm firebase-notification laravel-push-notifications

laravel-fcm's Introduction

Laravel FCM

A simple package that help you send a Firebase notification with your Laravel applications

Installation

You can pull the package via composer :

$ composer require kawankoding/laravel-fcm "^0.2.0"

Laravel

You must register the service provider :

// config/app.php

'Providers' => [
   // ...
   Kawankoding\Fcm\FcmServiceProvider::class,
]

If you want to make use of the facade you must install it as well :

// config/app.php

'aliases' => [
    // ...
    'Fcm' => Kawankoding\Fcm\FcmFacade::class,
];

Next, You must publish the config file to define your FCM server key :

php artisan vendor:publish --provider="Kawankoding\Fcm\FcmServiceProvider"

This is the contents of the published file :

return [

    /**
     * Set your FCM Server Key
     * Change to yours
     */

    'server_key' => env('FCM_SERVER_KEY', ''),

];

Lumen

Add the following service provider to the bootstrap/app.php file

$app->register(Kawankoding\Fcm\FcmServiceProvider::class);

Also copy the laravel-fcm.php config file to config/laravel-fcm.php

Add the configuration to the bootstrap/app.php file Important: this needs to be before the registration of the service provider

$app->configure('laravel-fcm');
...
$app->register(Kawankoding\Fcm\FcmServiceProvider::class);

Set your FCM Server Key in .env file :

APP_NAME="Laravel"
# ...
FCM_SERVER_KEY=putYourKeyHere

Methods Ref

  • ->to()

  • ->toTopic()

  • ->data()

  • ->notification()

  • ->priority()

  • ->timeToLive()

  • ->enableResponseLog()

  • ->send()

Usage

If You want to send a FCM with just notification parameter, this is an example of usage sending a FCM with only data parameter :

$recipients = [
    'clKMv.......',
    'GxQQW.......',
];

fcm()
    ->to($recipients)
    ->priority('high')
    ->timeToLive(0)
    ->data([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

NOTE: By default, Firebase server will queue your notification in 4 weeks. You could change this behavior by setting ->timeToLive(value_in_seconds). For example snippet above "->timeToLive(0)" will skip the queue, the target device (eg. android) must be online when the notification arive, otherwhise the target device will not receive the notification.

If You want to send a FCM to topic, use method toTopic($topic) instead to() :

fcm()
    ->toTopic($topic) // $topic must an string (topic name)
    ->priority('normal')
    ->timeToLive(0)
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

If You want to send a FCM with just notification parameter, this is an example of usage sending a FCM with only notification parameter :

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('high')
    ->timeToLive(0)
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

If You want to send a FCM with both data & notification parameter, this is an example of usage sending a FCM with both data & notification parameter :

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

Logging

To see the original response from Firebase, call enableResponseLog() method before calling the send() method.

fcm()
    ->to($recipients)
    // ...
    ->enableResponseLog()
    ->send();

Then you can check the response log in the file storage/logs/laravel.log

laravel-fcm's People

Contributors

akrindev avatar andriyandriyan avatar baggednismo avatar be-geeky avatar bjrnblm avatar crazyfreeman avatar defji avatar erlangp avatar erlangparasu avatar hayat-umar avatar ianmustafa avatar ilhamarrouf avatar killgt avatar mai92 avatar maulayyacyber avatar p-shakibafar avatar tamer-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-fcm's Issues

working fine on local but not on production

laravel-FCM notification working on local but not on production. My domain is ssl enabled. is it creating issue.

i try
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

but its still not working

here is my code...

$firebaseToken = User::where('id',$user->id)->whereNotNull('device_token')->pluck('device_token');

   $recipients =  $firebaseToken->toArray();
    fcm()
      ->to($recipients)
      ->priority('high')
      ->timeToLive(0)
      ->data([
          'title' => $title,
          'body'  => $body,
      ])
      ->send();   

Doesn't support laravel 5.5

Problem 1
    - Installation request for kawankoding/laravel-fcm ^0.0.7 -> satisfiable by kawankoding/laravel-fcm[0.0.7].
    - Conclusion: remove laravel/framework v5.5.18
    - Conclusion: don't install laravel/framework v5.5.18
...

Sending Result is null

When testing the library with 10-20 users, the result is an object and we are logging it to laravel.log
All works fine.

If we have 100+ tokens, the result is always null. How can we analyze what is happening

Notifications Channel

Hi, thanks for the hard work, I was wondering if there's a way to use in a Laravel Notification Channel? maybe toFCM($notifiable)

Thanks

do you know if this works with iphone device token and Firebase?

Hellow, first, thanks for share your work with us.

I dont have any response when i send notifications to an iPhone device. With php Curl the notifications works fine, but when i use FCM dosent send.

This is my code:

$fcm_token = User::select('fcm_token')->where('id',$request->partner_id)->first()->toArray();
$respuesta = fcm()
->to($fcm_token) // $recipients must an array
->priority('normal')
->timeToLive(0)
->data([
'sender_id' => intval($request->user_id),
'sender_name' => $user->name,
'type' => 'partner',
'date' => $request->date,
'shift' => $user->shift_id
])
->notification([
'title' => 'Test FCM',
'body' => 'This is a test of FCM',
])
->send();
->send();

And the return of json_encode($respuesta) is {}

Thanks again!!

Need an example

Hello,

Could you please help us in using this package, I did not get what recipient is.

fcm()
->to($recipients) // $recipients must an array
->data([
'title' => 'Test FCM',
'body' => 'This is a test of FCM',
])
->send();

Thanks

Lumen Throw Error

Hi, thank you for the package, but I get this error when implementing to Lumen 7.0
Call to undefined function Kawankoding\\Fcm\\config_path()

Can't composer update

First issue :)
Basically it's working nice, it's installing, after adding it to ServiceProviders it's also working nice but then I'm blocked because command composer update is throwing an error:

> php artisan clear-compiled

  [Symfony\Component\Debug\Exception\FatalThrowableError]
  Class 'Kawankoding\Fcm\Providers\FcmServiceProvider' not found

Script php artisan clear-compiled handling the post-update-cmd event returned with error code 1

Ga support laravel 5.6.x?

pass install di laravel 5.6 ga bisa

ini debugnya


  Problem 1
    - Installation request for kawankoding/laravel-fcm ^0.0.9 -> satisfiable by kawankoding/laravel-fcm[0.0.9].
    - Conclusion: remove laravel/framework v5.6.7
    - Conclusion: don't install laravel/framework v5.6.7
    - kawankoding/laravel-fcm 0.0.9 requires illuminate/support ~5.1.0|~5.2.0|~5.3.0|~5.4.0|~5.5.0 -> satisfiable by illuminate/support[5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36].
    - don't install illuminate/support 5.2.x-dev|don't install laravel/framework v5.6.7
    - don't install illuminate/support 5.3.x-dev|don't install laravel/framework v5.6.7
    - don't install illuminate/support 5.4.x-dev|don't install laravel/framework v5.6.7
    - don't install illuminate/support 5.5.x-dev|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.43|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.45|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.3.0|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.3.16|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.3.23|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.3.4|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.0|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.13|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.17|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.19|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.27|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.36|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.4.9|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.0|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.16|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.17|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.2|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.28|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.35|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.36|don't install laravel/framework v5.6.7
    - don't install illuminate/support 5.1.x-dev|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.1|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.13|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.16|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.2|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.20|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.22|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.25|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.28|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.30|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.31|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.41|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.6|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.1.8|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.0|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.19|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.21|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.24|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.25|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.26|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.27|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.28|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.31|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.32|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.37|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.6|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.2.7|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.33|don't install laravel/framework v5.6.7
    - don't install illuminate/support v5.5.34|don't install laravel/framework v5.6.7
    - Installation request for laravel/framework (locked at v5.6.7, required as 5.6.*) -> satisfiable by laravel/framework[v5.6.7].```

Not Support In Laravel 10

Not Support In Laravel 10
im trying to install it on laravel 10 but i found this error

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Root composer.json requires kawankoding/laravel-fcm ^0.2.7 -> satisfiable by kawankoding/laravel-fcm[0.2.7].
- kawankoding/laravel-fcm 0.2.7 requires illuminate/support ^5.1|^6.0|^7.0|^8.0|^9.0 -> found illuminate/support[v5.1.1, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.0] but these were not loaded, likely because it conflicts with another require.

You can also try re-running composer require with an explicit version constraint, e.g. "composer require kawankoding/laravel-fcm:*" to figure out if any version is installable, or "composer require kawankoding/laravel-fcm:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Push notification not working

Hello,

I'm trying to use this package, it's very simple and awesome, but it does not works,

Here is the code :
image

And the response in browser:
image

I did not receive any notification, when I try from firebase I got it :
image

Data Questions

Hi, thank you for the package, I was wondering if its possible to send additional data, for example buttons and actions in the push notifications, thanks for you work

polish chars problem

hi
everything works fin but when im sending text with polish characters message is not sending
is it possible to make some kind of debug on this fascade ?
send function

 FcmFacade::toTopic(env('FCM_TOPIC'))
        ->notification($data)
            ->send();

iOS not working

Hi guys, good day to you guys, first of all thank you soo much for the packages. This packages really make push notification process simple.

but i have issues on iOS, the notification is not being send. it works fine on Android, but doesnt work the same for iOS. May i know is there any configuration i need to make in order to make it works on iOS? Thank you sir.

p/s : Notification work fine on iOS if i blast it using Firebase Console.

get push notifications results

hello, i want to know how to get notification result. i want to check if notification sent successfully or error.

sorry for bad english
Thanks in advance

I used this package but u dont know why its not sent a notification? it's give me a blank page ?

//////////////////// method in controller ////////////////////
public function sendnotification(){
$registrationIds= array();
$recipients = Fcm_info::select('token')->get();

for ($i = 0, $c = count($recipients); $i < $c; ++$i) {
    array_push($registrationIds,  $recipients[$i]['token']);
    fcm()
          ->to($registrationIds) // $recipients must an array
          ->priority('normal')
          ->notification([
              'title' => 'Test FCM',
              'body' => 'This is a test of FCM',
          ])
          ->send();
 }

}
//////////////////// route ////////////////////

Route::get('/sendnotification','FcmController@sendnotification')->name('sendnotification');

Wrong payload data

Hello,

Just an FYI the payload flag has a typo in src/Fcm.php

$fields = [ '**content-available**' => true, 'priority' => 'high', 'data' => $this->data, 'notification' => $this->notification ];
should be
$fields = [ '**content_available**' => true, 'priority' => 'high', 'data' => $this->data, 'notification' => $this->notification ];

No support for laravel 4.2?

I tried to install the library in the laravel 4.2 project and it showed me error while i tried this command

$ composer require kawankoding/laravel-fcm

Is your library compatible with laravel 4.2 and php 5.6?

There is a limit for sending ?

Hello,

I have more than 1000 users, I want to send them a push, but seems it does not support this number,

I tried chunk, to send for example each 100, it send only for first chunk, any idea ?

Migrate from legacy FCM APIs to HTTP v1

Hi,
I get an email from Google where they tell me that I use legacy code for FCM:

We’re writing to let you know that starting June 20, 2024 the legacy Firebase Cloud Messaging (FCM) APIs will be discontinued.

What do you need to know?

On June 20, 2024, we’re reducing the number of Firebase Cloud Messaging (FCM) legacy register APIs and legacy send APIs that provide similar functionality. This step will allow us to provide you with a more consistent experience and align with Google security standards to improve security, reliability and performance.

Because of these API decommissions, some already-deprecated SDKs and features will stop working after June 20, 2024.

Please consult the tables below to find which Firebase Cloud Messaging (FCM) APIs and corresponding services/SDKs/features will be discontinued and replaced with new alternatives.

Discontinued Send API Decommissioned service New alternative
Legacy HTTP Protocol Sending messages via the Legacy HTTP API. Send messages via the HTTP v1 API.
XMPP Protocol Sending upstream and downstream messages via the XMPP API. Send downstream messages via the HTTP v1 API.Send upstream messages via HTTP/gRPC directly from the App to your server.
Batch send API Including multiple send requests in a single HTTP request to FCM known as Batch Send. Send messages via the HTTP v1 API, which has been optimized for fanout performance

We’re writing to let you know that starting June 20, 2024 the legacy Firebase Cloud Messaging (FCM) APIs will be discontinued.

What do you need to know?
On June 20, 2024, we’re reducing the number of Firebase Cloud Messaging (FCM) legacy register APIs and legacy send APIs that provide similar functionality. This step will allow us to provide you with a more consistent experience and align with Google security standards to improve security, reliability and performance.

Because of these API decommissions, some already-deprecated SDKs and features will stop working after June 20, 2024.

Please consult the tables below to find which Firebase Cloud Messaging (FCM) APIs and corresponding services/SDKs/features will be discontinued and replaced with new alternatives.

Discontinued Send API

Decommissioned service

New alternative

Legacy HTTP Protocol

Sending messages via the Legacy HTTP API.

Send messages via the HTTP v1 API.

XMPP Protocol

Sending upstream and downstream messages via the XMPP API.

Send downstream messages via the HTTP v1 API.

Send upstream messages via HTTP/gRPC directly from the App to your server.

Batch send API

Including multiple send requests in a single HTTP request to FCM known as Batch Send.

Send messages via the HTTP v1 API, which has been optimized for fanout performance

Discontinued Register API

Decommissioned SDK

New alternative

GCM register API

Google Cloud Messaging(GCM) SDKs (deprecated in 2018).

Latest FCM Android SDK.

Legacy Web register API

FCM JS SDK version<7.0.0 (deprecated in 2019)

Latest FCM JS SDK.

Instance ID Server API for Web

No SDK related to this API.

Latest FCM JS SDK.

Discontinued Feature

Decommissioned service

New alternative

Server keys

Authenticating requests with server keys.

Authenticate requests with access tokens generated from authorized service accounts

iOS Direct Channel

Sending messages via FCM’s direct channel to iOS devices while they are running in the foreground.

All iOS messages will be sent via APNS channel.

What do you need to do?
Take the following actions before June 20, 2024, to ensure that you have access to the latest supported features and to reduce the risk of future decommissions affecting your usage:

Follow the instructions described in Firebase FAQ to migrate your individual APIs.
Update to the latest versions of Firebase SDKs.
Platform

Recommended FCM SDK version

Android

= 23.1.2

iOS

= 10.10.0

Web (Javascript)

= 9.22.1

Then I a little bit take a look package and see that it use the old way to send Firebase FCM registration. Can you take a look at those URLs:
https://firebase.google.com/docs/cloud-messaging/migrate-v1?authuser=1
and
https://www.b4x.com/android/forum/threads/firebase-sending-tool-needs-update-to-support-firebase-cloud-messaging-v1.141044/#post-893722
and make an update or tell me which package I can use to migrate if you do not have the powers or time to make an update.

Best regards to the creators :)

Question: offline app

Hello,

What if I send a push to user that has not internet, once he connect, he'll not receive the push, how to fix that ?

Thank you

Error when trying to install or update package

Hi,
I am getting an error when I'm trying to update or install the package with composer.

composer require kawankoding/laravel-fcm

[RuntimeException]
Could not load package kawankoding/laravel-fcm in http://repo.packagist.org: [UnexpectedValueException] Could not p
arse version constraint ~5.1.x: Invalid version string "~5.1.x"

[UnexpectedValueException]
Could not parse version constraint ~5.1.x: Invalid version string "~5.1.x"

Thank you.

Sound in notifications?

Hello! I would like to know if the sound of the notifications can be configured, at least with a default sound, since when the notifications arrive they do not sound.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.