PHP code example of bilalmardini / firebase-notification
1. Go to this page and download the library: Download bilalmardini/firebase-notification library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bilalmardini / firebase-notification example snippets
'providers' => [
// Other Service Providers
BilalMardini\FirebaseNotification\Providers\FirebaseNotificationServiceProvider::class,
],
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use BilalMardini\FirebaseNotification\Facades\FirebaseNotification;
class NewsNotificationController extends Controller
{
/**
* Send a global news update to users subscribed to 'global-news'.
*
* @return \Illuminate\Http\JsonResponse
*/
public function sendGlobalNewsUpdate()
{
$result = FirebaseNotification::setTitle('Breaking News')
->setBody('A major event just happened. Click to read more.')
->setIcon('https://news-website.com/news-icon.png')
->setTopic('global-news') // Target 'global-news' topic
->setData(['news_id' => 5678, 'category' => 'breaking-news'])
->push();
return $result
? response()->json(['message' => 'News notification sent successfully.'], 200)
: response()->json(['message' => 'Failed to send news notification.'], 500);
}
}
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use BilalMardini\FirebaseNotification\Facades\FirebaseNotification;
class PromotionNotificationController extends Controller
{
/**
* Notify users about a new promotional offer.
*
* @return \Illuminate\Http\JsonResponse
*/
public function sendPromotionNotification()
{
$eligibleUsers = User::where('is_eligible_for_promo', true)->get();
$result = FirebaseNotification::setTitle('Exclusive Promotion Just for You!')
->setBody('Unlock your special offer now. Limited time only!')
->setIcon('https://yourstore.com/promo-icon.png')
->setUsers($eligibleUsers) // Target specific users
->setData(['promo_code' => 'PROMO2024', 'discount' => '20%'])
->push();
return $result
? response()->json(['message' => 'Promotion notification sent successfully.'], 200)
: response()->json(['message' => 'Failed to send promotion notification.'], 500);
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $fillable = [
'title_en',
'title_ar',
'description_en',
'description_ar',
'is_general',
];
public function userNotifications()
{
return $this->hasMany(UserNotification::class);
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class UserNotification extends Model
{
use HasFactory;
protected $fillable = [
'notification_id',
'user_id',
];
public function notification()
{
return $this->belongsTo(Notification::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class UserFcmToken extends Model
{
use HasFactory;
protected $fillable = ['user_id', 'fcm_token'];
public function user()
{
return $this->belongsTo(User::class);
}
}