Bạn đang không biết làm thế nào để gửi push notification cho app androi, ios thì đây chính là bài viết dành cho bạn. Mình sẽ hướng dẫn các bạn xây dược thư viện để gửi push notification sử dụng firebase và laravel.
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_id | bigint(20) unsigned | YES | MUL | NULL | |
| device_token | varchar(255) | NO | | NULL | |
| device_id | varchar(255) | YES | MUL | NULL | |
| device_type | varchar(25) | NO | | ios | |
| active | tinyint(1) | NO | MUL | 1 | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
App/Service/FCMService.php
<?php
namespace App\Services;
use GuzzleHttp\Client;
use Log;
class FCMService
{
private $apiConfig;
public function __construct()
{
$this->apiConfig = [
'url' => config('firebase.push_url'),
'server_key' => config('firebase.server_key'),
'device_type' => config('firebase.device_type')
];
}
/**
* Sending push message to single user by Firebase
*
* @param string $device_token
* @param array $notification
* @param array $data
*
* @return bool|string
*/
public function send(string $device_token, array $notification, array $data)
{
if ($data['device_type'] === $this->apiConfig['device_type']['ios']) {
$fields = [
'to' => $device_token,
'notification' => $notification,
'data' => $data
];
} else {
$fields = [
'to' => $device_token,
'data' => array_merge($data, $notification)
];
}
return $this->sendPushNotification($fields);
}
/**
* Sending push message to multiple users by firebase
* @param array $device_tokens
* @param array $notification
* @param array $data
*
* @return bool|string
*/
public function sendMultiple(array $device_tokens, array $notification, array $data)
{
$fields = [
'registration_ids' => $device_tokens,
'data' => $data,
'notification' => $notification
];
return $this->sendPushNotification($fields);
}
/**
* GuzzleHTTP request to firebase servers
* @param array $fields
*
* @return bool
*/
private function sendPushNotification(array $fields)
{
$client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'key='. $this->apiConfig['server_key'],
]
]);
$res = $client->post(
$this->apiConfig['url'],
['body' => json_encode($fields)]
);
// Request api fcm by json data
// $res = $client->post($this->apiConfig['url'], json_encode($fields), ['type' => 'json']);
$res = json_decode($res->getBody());
if ($res->failure) {
Log::error("ERROR_PUSH_NOTIFICATION: ".$fields['to']);
}
return true;
}
}
config/firebase.php
<?php
return [
'push_url' => 'https://fcm.googleapis.com/fcm/send',
'server_key' => env('FIREBASE_SERVER_KEY', null), // Lấy trong tài khoản firebase
'device_type' => [
'ios' => 'iOS',
'android' => 'android'
],
'sound' => 'default'
];
App/Traists/PushNotificationTraist.php
<?php
namespace App\Traits;
use App\Services\FCMService;
trait PushNotificationTrait
{
public function pushMessage(string $deviceToken, array $notification, array $data)
{
$pushNotificationService = new FCMService();
return $pushNotificationService->send($deviceToken, $notification, $data);
}
public function pushMessages(array $deviceTokens, array $notification, array $data)
{
$pushNotificationService = new FCMService();
return $pushNotificationService->sendMultiple($deviceTokens, $notification, $data);
}
}
App/Http/Controllers/NotificationController.php
<?php
namespace App\Http\Controllers;
use App\Traits\PushNotificationTrait;
class HomeController extends Controller
{
use PushNotificationTrait;
public function sendPush()
{
$totalUnread = 1;
$data = [
'func_name' => config('firebase.notification.func'),
'screen' => config('firebase.notification.screen'),
'total_unread' => $totalUnread,
'total_count' => 2,
'device_type' => 'ios', // Loại device, có thể là androi, web, ios
];
$content = [
"title" => "hello", // tiêu đề tin nhắn
"body" => "test", // nội dung tin nhắn
'badge' => $totalUnread, // số message chưa đọc
'sound' => config('firebase.sound') // âm báo tin nhắn
];
// Push notification
$this->pushMessage($deviceToken, $content, $data);
}
}