Laravel package to send push notification to mobile devices (android, ios) using fcm http v1
kobir/larapush-notification is a Laravel package for laravel package to send push notification to mobile devices (android, ios) using fcm http v1.
It currently has 2 GitHub stars and 19 downloads on Packagist (latest version v1.0.1).
Install it with composer require kobir/larapush-notification.
Discover more Laravel packages by kobir
or browse all Laravel packages to compare alternatives.
Last updated
Simple Laravel package for sending push notification to mobile devices (android, ios) using fcm http v1.
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"
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:
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 method use to set the notification basic parameters, which you pass the values through parameter as an array.
Syntax
object setNotification(array $data)
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 method use to set the Firebase Project ID of your App as a string.
Syntax
object setProjectId($projectId)
setJsonCredential method use to set the path of credentials json file of your App.
Syntax
object setJsonCredential($filePath)
setConfig method sets the Push service configuration, which you pass the name through parameter as an array.
Syntax
object setConfig(array $config)
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 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 method is responsible for sending the push notification.
Syntax
object send()
getFeedback method return the full notification response after sending a notification.
Syntax
object getFeedback()
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 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 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 method use to remove devices from a topic.
Syntax
object removeDeviceTokenFromTopic(string $topic)
getTopicInfo method use to get all topics name associate with the provided device token.
Syntax
object getTopicInfo(string $deviceToken)
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 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 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.
$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.
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();
$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();
/***
* (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();
$response = $push->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
->addDeviceTokenToTopic('foo');
$response = $push->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
->removeDeviceTokenFromTopic('bar');
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"
}
}
}
}