kobir / larapush-notification
Laravel package to send push notification to mobile devices (android, ios) using fcm http v1
Requires
- php: ^7.2|^7.3|^7.4|^8.0|^8.1|^8.2
- google/apiclient: ^2.14.0|2.15.0
- guzzlehttp/guzzle: ^6.3|^7.0|^7.2
- illuminate/notifications: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^7.5|^8.0|^9.0|^9.3|^9.5|^10.5
This package is auto-updated.
Last update: 2026-02-25 20:24:39 UTC
README
Simple Laravel package for sending push notification to mobile devices (android, ios) using fcm http v1.
Requirements
- php version ^7.2 or higher
- Google API Client Library ^2.14.0 or higher
- Laravel version ^6.0 or higher
Installation
Use composer to download and install the package and its dependencies.
composer require kobir/larapush-notification
if you are facing dependency version conflict related issues please run
composer require kobir/larapush-notification -W
The package will automatically register its service provider.
To publish the package's configuration file run the following command
php artisan vendor:publish --provider="Kobir\PushNotification\Providers\PushNotificationServiceProvider" --tag="config"
Configuration
After publishing the configuration, you can find the 'larapushnotification.php' as well as 'fcmCertificates' empty directory into the config folder.
The default configuration parameters for FCM are :
'priority' => 'normal','certificate' => __DIR__ . '/fcmCertificates/fcm-admin-sdk.json','dry_run' => false,'firebase_project_id' => 'FIREBASE_PROJECT_ID','token_cache_time' => 3500, //in seconds & must be <= 3500'guzzle' => [],
You can dynamically update those values or adding new ones calling the method setConfig like so:
$push->setConfig([ 'priority' => 'high', 'firebase_project_id' => 'my-project-id', 'certificate' => 'path/to/fcm-admin-sdk.json' 'token_cache_time' => 3000 ]);
To generate a credentials fcm-admin-sdk json file for your service account:
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- Securely store the JSON file containing the key.
Usage
To instantiate the Push Notification class
$push = new PushNotification;
By default it will use fcm as Push Service provider or you can pass service name if you want as like...
$push = new PushNotification('fcm');
Now you may use any method that you need. Please see the all methods list.
setNotification
setNotification method use to set the notification basic parameters, which you pass the values through parameter as an array.
Syntax
object setNotification(array $data)
setData
setData method use to set the additional message parameters like id, type sound click_action etc.., which you pass the values through parameter as an array.
Syntax
object setData(array $data)
setProjectId
setProjectId method use to set the Firebase Project ID of your App as a string.
Syntax
object setProjectId($projectId)
setJsonCredential
setJsonCredential method use to set the path of credentials json file of your App.
Syntax
object setJsonCredential($filePath)
setConfig
setConfig method sets the Push service configuration, which you pass the name through parameter as an array.
Syntax
object setConfig(array $config)
setUrl
setUrl method sets the Push service url, which you pass the url through parameter as a string.
Syntax
object setUrl($url)
Not update the url unless it's really necessary.
setDevicesToken
setDevicesToken method use to set the devices' tokens, which you pass the token through parameter either array for multiple tokens or string if single token.
Syntax
object setDevicesToken($deviceTokens)
send
send method is responsible for sending the push notification.
Syntax
object send()
getFeedback
getFeedback method return the full notification response after sending a notification.
Syntax
object getFeedback()
getFailedDeviceTokens
getFailedDeviceTokens method return the all devices' tokens that couldn't receive the notification or failed. Further you can make logic to your backend code for that device tokens.
Syntax
array getFailedDeviceTokens()
sendByTopic
sendByTopic method use to send notification by topic. It also accepts the topic condition. more details here
If isCondition is true, $topic will be treated as an expression
Syntax
object sendByTopic($topic, $isCondition)
addDeviceTokenToTopic
addDeviceTokenToTopic method use to add device into a topic. To add a new topic you need a unique topic name & all devices would you want to add this topic.
Syntax
object addDeviceTokenToTopic(string $topic)
removeDeviceTokenFromTopic
removeDeviceTokenFromTopic method use to remove devices from a topic.
Syntax
object removeDeviceTokenFromTopic(string $topic)
getTopicInfo
getTopicInfo method use to get all topics name associate with the provided device token.
Syntax
object getTopicInfo(string $deviceToken)
setTopicAddUrl
setTopicAddUrl method sets the topic add url, which you pass the url through parameter as a string.
Syntax
object setTopicAddUrl($url)
Not update the url unless it's really necessary.
setTopicRemoveUrl
setTopicRemoveUrl method sets the topic remove url, which you pass the url through parameter as a string.
Syntax
object setTopicRemoveUrl($url)
Not update the url unless it's really necessary.
setTopicInfoUrl
setTopicInfoUrl method sets the topic info url, which you pass the url through parameter as a string.
Syntax
object setTopicInfoUrl($url)
Not update the url unless it's really necessary.
Usage samples
$push = new PushNotification(); /*** * (string) title * (string) body * (string) image, optional field image size must be less than 1mb */ $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional, ]) ->setDevicesToken(['deviceToken1', 'deviceToken2', ...]) ->send();
This is the basic data-set for notification. Here send() method used for sending push notification.
Additional Payload
You can also pass the additional payload data by using setData() method
/*** * (string) id, if haven't id place '0' */ $feedback = $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional ]) ->setData([ 'id' => (string) 'dynamic_id', 'type' => 'OFFER', 'sound' => 'default', 'click_action' => 'NOTIFICATION_CLICK', ]) ->setDevicesToken(['deviceToken1', 'deviceToken2', ...]) ->send() ->getFeedback();
Get Notification Feedback
$feedback = $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional ]) ->setDevicesToken(['deviceToken1', 'deviceToken2', ...]) ->send() ->getFeedback();
If you want get the failed device tokens use getFailedDeviceTokens() method as ...
$failedTokens = $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional ]) ->setDevicesToken(['deviceToken1', 'deviceToken2', ...]) ->send() ->getFailedDeviceTokens();
If you want send the notification to only 1 device, you may pass the value as string.
$feedback = $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional ]) ->setDevicesToken('deviceToken') ->send() ->getFeedback();
Send Notification by Topic
/*** * (string) topic, name like 'foo', 'bar' */ $feedback = $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional ]) ->sendByTopic('bar') ->getFeedback();
or with using a condition:
/*** * you must have pass the additional parameter as 'true' */ $feedback = $push->setNotification([ 'title' => (string) 'Title goes here', 'body' => (string) 'Body text goes here', 'image' => (string) 'image-url', //optional ]) ->sendByTopic("'foo' in topics || 'bar' in topics", true) ->getFeedback();
Add Device Tokens into Topic
$response = $push->setDevicesToken(['deviceToken1', 'deviceToken2', ...]) ->addDeviceTokenToTopic('foo');
Remove Device Tokens from Topic
$response = $push->setDevicesToken(['deviceToken1', 'deviceToken2', ...]) ->removeDeviceTokenFromTopic('bar');
Get Topic Information
If you want to get how many topic associated with a device token then call this getTopicInfo() method with device_token parameter.
$response = $push->getTopicInfo('deviceToken1'); //successful response will be like { "applicationVersion": "19", "application": "com.iid.example", "authorizedEntity": "123456782354", "platform": "ANDROID", "rel": { "topics": { "cats": { "addDate": "2024-10-20" }, "dogs": { "addDate": "2024-10-20" } } } }